結果

問題 No.2563 色ごとのグループ
ユーザー K5h1n0
提出日時 2023-12-02 16:12:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 660 ms / 2,000 ms
コード長 734 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 130,688 KB
最終ジャッジ日時 2024-09-26 19:57:31
合計ジャッジ時間 11,381 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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