結果
問題 | No.2563 色ごとのグループ |
ユーザー | けんぴん |
提出日時 | 2023-09-23 20:00:09 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,083 bytes |
コンパイル時間 | 303 ms |
コンパイル使用メモリ | 82,024 KB |
実行使用メモリ | 124,836 KB |
最終ジャッジ日時 | 2024-09-26 16:31:42 |
合計ジャッジ時間 | 9,064 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
55,776 KB |
testcase_01 | AC | 39 ms
55,264 KB |
testcase_02 | AC | 41 ms
54,936 KB |
testcase_03 | AC | 41 ms
55,768 KB |
testcase_04 | AC | 42 ms
53,984 KB |
testcase_05 | RE | - |
testcase_06 | AC | 43 ms
54,528 KB |
testcase_07 | RE | - |
testcase_08 | AC | 44 ms
54,144 KB |
testcase_09 | AC | 47 ms
55,424 KB |
testcase_10 | AC | 43 ms
54,144 KB |
testcase_11 | AC | 47 ms
54,784 KB |
testcase_12 | AC | 45 ms
54,528 KB |
testcase_13 | AC | 45 ms
54,528 KB |
testcase_14 | AC | 82 ms
74,752 KB |
testcase_15 | AC | 88 ms
77,312 KB |
testcase_16 | AC | 82 ms
75,008 KB |
testcase_17 | AC | 88 ms
74,624 KB |
testcase_18 | AC | 61 ms
65,152 KB |
testcase_19 | AC | 97 ms
77,312 KB |
testcase_20 | AC | 107 ms
79,424 KB |
testcase_21 | AC | 100 ms
78,720 KB |
testcase_22 | AC | 100 ms
77,824 KB |
testcase_23 | AC | 110 ms
80,128 KB |
testcase_24 | AC | 261 ms
102,848 KB |
testcase_25 | AC | 224 ms
100,096 KB |
testcase_26 | AC | 284 ms
108,588 KB |
testcase_27 | AC | 249 ms
110,912 KB |
testcase_28 | AC | 331 ms
110,700 KB |
testcase_29 | AC | 384 ms
124,520 KB |
testcase_30 | AC | 383 ms
124,836 KB |
testcase_31 | AC | 387 ms
124,680 KB |
testcase_32 | AC | 387 ms
124,540 KB |
testcase_33 | AC | 591 ms
116,048 KB |
testcase_34 | AC | 581 ms
117,612 KB |
testcase_35 | AC | 586 ms
116,112 KB |
testcase_36 | AC | 582 ms
116,836 KB |
testcase_37 | AC | 549 ms
116,052 KB |
ソースコード
from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) def main(): N,M = map(int,input().split()) C = list(map(int,input().split())) C_dict = defaultdict(list) assert 1 <= N <= 2*10**5 assert 1 <= M <= min(2*10**5, N*(N-1)//2) for i,c in enumerate(C): assert 1 <= c <= N C_dict[c].append(i) uv_set = set() uf = UnionFind(N) for _ in [0]*M: u,v = map(int,input().split()) assert u != v assert (u,v) not in uv_set assert (v,u) not in uv_set assert 1 <= u <= N assert 1 <= v <= N uv_set.add((u,v)) if C[u-1] == C[v-1]: uf.union(u-1,v-1) # print(C_dict) # print(uf) ans = 0 for c in C_dict: set_per_c = set() for i in C_dict[c]: set_per_c.add(uf.find(i)) ans += len(set_per_c)-1 print(ans) if __name__ == "__main__": main()