結果

問題 No.2563 色ごとのグループ
ユーザー AngrySadEightAngrySadEight
提出日時 2023-11-24 00:39:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 883 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 17,172 KB
最終ジャッジ日時 2023-12-02 14:01:27
合計ジャッジ時間 6,592 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
9,984 KB
testcase_01 AC 28 ms
9,984 KB
testcase_02 AC 26 ms
9,984 KB
testcase_03 AC 27 ms
9,984 KB
testcase_04 AC 27 ms
9,984 KB
testcase_05 AC 28 ms
9,984 KB
testcase_06 AC 29 ms
9,984 KB
testcase_07 AC 26 ms
9,984 KB
testcase_08 AC 27 ms
9,984 KB
testcase_09 AC 32 ms
9,984 KB
testcase_10 AC 28 ms
9,984 KB
testcase_11 AC 31 ms
9,984 KB
testcase_12 AC 28 ms
9,984 KB
testcase_13 AC 28 ms
9,984 KB
testcase_14 AC 234 ms
10,368 KB
testcase_15 AC 556 ms
10,368 KB
testcase_16 AC 550 ms
10,240 KB
testcase_17 AC 464 ms
10,240 KB
testcase_18 AC 33 ms
10,368 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N, M = map(int, input().split())
C = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    for i in range(M):
        if C[u] == C[v]:
            graph[u].append(v)
            graph[v].append(u)

ans = 0
Clist = [[] for _ in range(N + 1)]
for i in range(N):
    Clist[C[i]].append(i)
isvisited = [False for _ in range(N)]
dq = deque()
for i in range(1, N + 1):
    comp = 0
    for v in Clist[i]:
        if isvisited[v]:
            continue
        comp += 1
        isvisited[v] = True
        dq.append(v)
        while len(dq):
            p = dq.popleft()
            for x in graph[p]:
                if isvisited[x]:
                    continue
                isvisited[x] = True
                dq.append(x)
    ans += max(0, comp - 1)
print(ans)
0