結果
| 問題 | No.2563 色ごとのグループ |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-08 00:59:06 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 271 ms / 2,000 ms |
| コード長 | 845 bytes |
| 記録 | |
| コンパイル時間 | 348 ms |
| コンパイル使用メモリ | 85,504 KB |
| 実行使用メモリ | 116,308 KB |
| 最終ジャッジ日時 | 2026-04-13 23:07:12 |
| 合計ジャッジ時間 | 6,467 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
class UnionFind:
def __init__(self, N):
self.p = [-1] * N
def find(self, x):
y = self.p[x]
while y >= 0:
x = y
y = self.p[y]
return x
def unite(self, x, y):
x, y = self.find(x), self.find(y)
if x == y:
return
if -self.p[x] < -self.p[y]:
x, y = y, x
self.p[x] += self.p[y]
self.p[y] = x
N, M = map(int, input().split())
uf = UnionFind(N)
ans = 0
C = list(map(int, input().split()))
for _ in range(M):
v, w = map(int, input().split())
v -= 1
w -= 1
if C[v] != C[w]:
continue
uf.unite(v, w)
d = {}
for i, c in enumerate(C):
if c not in d:
d[c] = []
d[c].append(i)
for c in d:
S = set()
for i in d[c]:
S.add(uf.find(i))
ans += len(S) - 1
print(ans)