結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
kemuniku
|
| 提出日時 | 2023-12-02 15:02:32 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 406 ms / 2,000 ms |
| コード長 | 1,157 bytes |
| コンパイル時間 | 288 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 122,376 KB |
| 最終ジャッジ日時 | 2024-09-26 17:57:02 |
| 合計ジャッジ時間 | 7,233 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
class UnionFind():
def __init__(self, n):
self.count = n
self.par = [-1]*n
self.siz = [1]*n
def root(self,x):
if(self.par[x] == -1):
return x
else:
self.par[x] = self.root(self.par[x])
return self.par[x]
def issame(self,x,y):
return self.root(x) == self.root(y)
def unite(self,x,y):
x = self.root(x)
y = self.root(y)
if(x == y):
return False
if(self.siz[x]<self.siz[y]):
x,y = y,x
self.par[y] = x
self.siz[x] += self.siz[y]
self.count -= 1
return True
def size(self,x):
return self.siz[self.root(x)]
from collections import defaultdict
N,M = map(int,input().split())
C = list(map(int,input().split()))
d = defaultdict(int)
for i in range(N):
d[C[i]] += 1
cost = {}
for c in d:
cost[c] = d[c]-1
uf = UnionFind(N)
for i in range(M):
u,v = map(int,input().split())
u-=1
v-=1
if C[u] == C[v]:
if not uf.issame(u,v):
uf.unite(u,v)
cost[C[u]] -= 1
ans = 0
for c in d:
x = cost[c]
ans += x
print(ans)
kemuniku