結果

問題 No.2563 色ごとのグループ
ユーザー K5h1n0K5h1n0
提出日時 2023-12-02 16:12:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 543 ms / 2,000 ms
コード長 734 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 130,332 KB
最終ジャッジ日時 2023-12-02 16:12:27
合計ジャッジ時間 9,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
55,596 KB
testcase_01 AC 35 ms
55,596 KB
testcase_02 AC 35 ms
55,596 KB
testcase_03 AC 38 ms
55,596 KB
testcase_04 AC 35 ms
55,596 KB
testcase_05 AC 35 ms
55,596 KB
testcase_06 AC 35 ms
55,596 KB
testcase_07 AC 36 ms
55,596 KB
testcase_08 AC 38 ms
55,596 KB
testcase_09 AC 41 ms
55,596 KB
testcase_10 AC 37 ms
55,596 KB
testcase_11 AC 39 ms
55,596 KB
testcase_12 AC 37 ms
55,596 KB
testcase_13 AC 39 ms
55,596 KB
testcase_14 AC 71 ms
74,252 KB
testcase_15 AC 75 ms
76,680 KB
testcase_16 AC 68 ms
73,612 KB
testcase_17 AC 66 ms
73,740 KB
testcase_18 AC 48 ms
66,640 KB
testcase_19 AC 72 ms
76,684 KB
testcase_20 AC 104 ms
80,464 KB
testcase_21 AC 99 ms
77,356 KB
testcase_22 AC 85 ms
77,024 KB
testcase_23 AC 92 ms
77,124 KB
testcase_24 AC 276 ms
101,624 KB
testcase_25 AC 251 ms
105,320 KB
testcase_26 AC 337 ms
124,092 KB
testcase_27 AC 329 ms
125,932 KB
testcase_28 AC 398 ms
130,332 KB
testcase_29 AC 504 ms
126,392 KB
testcase_30 AC 541 ms
126,392 KB
testcase_31 AC 523 ms
126,392 KB
testcase_32 AC 543 ms
126,392 KB
testcase_33 AC 455 ms
117,716 KB
testcase_34 AC 462 ms
116,856 KB
testcase_35 AC 448 ms
116,768 KB
testcase_36 AC 439 ms
117,216 KB
testcase_37 AC 489 ms
117,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque

n,m = map(int,input().split())
c = list(map(int,input().split()))
cd = defaultdict(int)
for i in list(set(c)):
    cd[i] = 0
c = [0] + c
d = defaultdict(list)
for i in range(m):
    a,b = map(int,input().split())
    d[a].append(b)
    d[b].append(a)

visited = [False]*(n+1)
for i in range(1,n+1):
    if visited[i]:
        continue
    q = deque()
    nowcolor = c[i]
    q.append(i)
    visited[i] = True
    while q:
        nowv = q.popleft()
        for nextv in d[nowv]:
            if c[nextv] == nowcolor and visited[nextv] == False:
                visited[nextv] = True
                q.append(nextv)
    cd[nowcolor] += 1
ans = 0
for i in cd.values():
    ans += i-1
print(ans)
0