結果
問題 | No.2563 色ごとのグループ |
ユーザー |
![]() |
提出日時 | 2023-12-02 16:00:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 536 ms / 2,000 ms |
コード長 | 640 bytes |
コンパイル時間 | 395 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 152,132 KB |
最終ジャッジ日時 | 2024-09-26 19:39:57 |
合計ジャッジ時間 | 8,519 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
N, M = map(int, input().split()) C = list(map(int, input().split())) UV = [map(int, input().split()) for _ in range(M)] graph = [[] for _ in range(N)] for U, V in UV: U -= 1 V -= 1 if C[U] == C[V]: graph[U].append(V) graph[V].append(U) ac = [False] * N col = [False] * N ans = 0 for i in range(N): if ac[i]: continue if col[C[i]-1]: ans += 1 col[C[i]-1] = True q = [i] while q: now = q.pop() if ac[now]: continue ac[now] = True for g in graph[now]: if ac[g]: continue q.append(g) print(ans)