結果
| 問題 | No.2563 色ごとのグループ | 
| コンテスト | |
| ユーザー |  Koi | 
| 提出日時 | 2023-12-02 15:37:24 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 526 ms / 2,000 ms | 
| コード長 | 585 bytes | 
| コンパイル時間 | 403 ms | 
| コンパイル使用メモリ | 82,312 KB | 
| 実行使用メモリ | 123,120 KB | 
| 最終ジャッジ日時 | 2024-09-26 19:01:17 | 
| 合計ジャッジ時間 | 8,070 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 | 
ソースコード
from collections import defaultdict,deque
N,M=map(int,input().split())
C=list(map(int,input().split()))
G=defaultdict(list)
for i in range(M):
    u,v=map(int,input().split())
    if(C[u-1]==C[v-1]):
        G[u].append(v)
        G[v].append(u)
#連結成分の個数-色の種類数
T=1
c=0
seen=[False]*(N+1)
while T<=N:
    que=deque([T])
    seen[T]=True
    while len(que):
        p=que.popleft()
        for q in G[p]:
            if(not seen[q]):
                seen[q]=True
                que.append(q)
    while T<=N and seen[T]:
        T+=1
    c+=1
print(c-len(set(C)))
            
            
            
        