結果
| 問題 | No.2563 色ごとのグループ |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-08 00:59:06 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 357 ms / 2,000 ms |
| コード長 | 845 bytes |
| コンパイル時間 | 278 ms |
| コンパイル使用メモリ | 82,560 KB |
| 実行使用メモリ | 110,848 KB |
| 最終ジャッジ日時 | 2024-09-27 02:30:52 |
| 合計ジャッジ時間 | 7,855 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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)