結果
問題 | No.2563 色ごとのグループ |
ユーザー |
|
提出日時 | 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 |
ソースコード
from collections import dequeN, 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:continuevis.add(s_id)FIFO = deque()FIFO.append(s_id)color = C[s_id - 1]cnt[color] += 1while 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 = 0for i in range(len(cnt)):if cnt[i] >= 2:ans += cnt[i] - 1print(ans)