結果

問題 No.2563 色ごとのグループ
ユーザー u_kunu_kun
提出日時 2023-12-02 16:13:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 520 ms / 2,000 ms
コード長 861 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 131,080 KB
最終ジャッジ日時 2024-09-26 19:59:27
合計ジャッジ時間 8,965 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N, M = map(int, input().split())
C = list(map(int, input().split()))

#色iの連結成分の個数はcnt[i]個
cnt = [0 for _ in range(N + 1)]

G = [[] for _ in range(N + 1)]

for _ in range(M):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)

vis = set()

for s_id in range(1, N + 1):
    if s_id in vis:
        continue
    
    vis.add(s_id)
    FIFO = deque()
    FIFO.append(s_id)
    
    color = C[s_id - 1]
    
    cnt[color] += 1
    
    while FIFO:
        now_vid = FIFO.popleft()
        
        for ne_vid in G[now_vid]:
            if ne_vid not in vis and C[now_vid - 1] == C[ne_vid - 1]:
                vis.add(ne_vid)
                FIFO.append(ne_vid)
                
        
ans = 0
for i in range(len(cnt)):
    if cnt[i] >= 2:
        ans += cnt[i] - 1

print(ans)








0