結果
問題 | No.2563 色ごとのグループ |
ユーザー | torippy |
提出日時 | 2023-12-02 15:17:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 538 ms / 2,000 ms |
コード長 | 2,311 bytes |
コンパイル時間 | 301 ms |
コンパイル使用メモリ | 82,368 KB |
実行使用メモリ | 178,016 KB |
最終ジャッジ日時 | 2024-09-26 18:23:16 |
合計ジャッジ時間 | 9,854 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
54,272 KB |
testcase_01 | AC | 38 ms
54,528 KB |
testcase_02 | AC | 39 ms
54,400 KB |
testcase_03 | AC | 39 ms
54,144 KB |
testcase_04 | AC | 39 ms
54,400 KB |
testcase_05 | AC | 39 ms
54,528 KB |
testcase_06 | AC | 38 ms
54,272 KB |
testcase_07 | AC | 40 ms
54,016 KB |
testcase_08 | AC | 39 ms
54,656 KB |
testcase_09 | AC | 42 ms
55,552 KB |
testcase_10 | AC | 38 ms
54,272 KB |
testcase_11 | AC | 42 ms
55,552 KB |
testcase_12 | AC | 41 ms
55,168 KB |
testcase_13 | AC | 39 ms
54,528 KB |
testcase_14 | AC | 83 ms
77,784 KB |
testcase_15 | AC | 84 ms
77,688 KB |
testcase_16 | AC | 83 ms
77,696 KB |
testcase_17 | AC | 108 ms
77,312 KB |
testcase_18 | AC | 68 ms
73,728 KB |
testcase_19 | AC | 87 ms
77,568 KB |
testcase_20 | AC | 117 ms
84,568 KB |
testcase_21 | AC | 118 ms
79,744 KB |
testcase_22 | AC | 116 ms
79,360 KB |
testcase_23 | AC | 111 ms
80,128 KB |
testcase_24 | AC | 288 ms
127,644 KB |
testcase_25 | AC | 256 ms
117,524 KB |
testcase_26 | AC | 387 ms
155,584 KB |
testcase_27 | AC | 418 ms
159,308 KB |
testcase_28 | AC | 387 ms
157,460 KB |
testcase_29 | AC | 495 ms
177,604 KB |
testcase_30 | AC | 476 ms
172,312 KB |
testcase_31 | AC | 487 ms
177,500 KB |
testcase_32 | AC | 496 ms
172,532 KB |
testcase_33 | AC | 509 ms
178,016 KB |
testcase_34 | AC | 523 ms
177,592 KB |
testcase_35 | AC | 526 ms
177,960 KB |
testcase_36 | AC | 537 ms
177,652 KB |
testcase_37 | AC | 538 ms
177,716 KB |
ソースコード
from collections import defaultdict from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): assert 0 <= x if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x 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()) N, M = map(int, input().split()) C = list(map(int, input().split())) u = [] v = [] for i in range(M): ui, vi = map(int, input().split()) ui -= 1 vi -= 1 u.append(ui) v.append(vi) Ccount = defaultdict(int) ColorV = [[] for _ in range(N)] for i in range(N): C[i] -= 1 Ccount[C[i]] += 1 ColorV[C[i]].append(i) ColorUFdict: "dict[int, UnionFind]" = {} v2i = defaultdict(int) # i: color for i in range(N): if Ccount[i] == 0: continue uf = UnionFind(Ccount[i]) ColorUFdict[i] = uf for j in range(len(ColorV[i])): v2i[ColorV[i][j]] = j for i in range(M): if C[u[i]] == C[v[i]]: ColorUFdict[C[u[i]]].unite(v2i[u[i]], v2i[v[i]]) ans = 0 for i in range(N): if Ccount[i] == 0: continue fr = v2i[ColorV[i][0]] uf = ColorUFdict[i] for x in ColorV[i]: to = v2i[x] if not uf.same(fr, to): ans += 1 uf.unite(fr, to) print(ans)