結果
問題 | No.1390 Get together |
ユーザー | FromBooska |
提出日時 | 2023-03-11 18:17:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 451 ms / 2,000 ms |
コード長 | 1,855 bytes |
コンパイル時間 | 403 ms |
コンパイル使用メモリ | 81,680 KB |
実行使用メモリ | 102,936 KB |
最終ジャッジ日時 | 2023-10-18 10:10:59 |
合計ジャッジ時間 | 10,462 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
55,564 KB |
testcase_01 | AC | 43 ms
55,564 KB |
testcase_02 | AC | 42 ms
55,564 KB |
testcase_03 | AC | 99 ms
76,808 KB |
testcase_04 | AC | 88 ms
76,504 KB |
testcase_05 | AC | 93 ms
76,772 KB |
testcase_06 | AC | 90 ms
76,636 KB |
testcase_07 | AC | 87 ms
76,516 KB |
testcase_08 | AC | 88 ms
76,620 KB |
testcase_09 | AC | 95 ms
76,812 KB |
testcase_10 | AC | 44 ms
55,564 KB |
testcase_11 | AC | 43 ms
55,564 KB |
testcase_12 | AC | 43 ms
55,564 KB |
testcase_13 | AC | 43 ms
55,564 KB |
testcase_14 | AC | 43 ms
55,564 KB |
testcase_15 | AC | 43 ms
55,564 KB |
testcase_16 | AC | 191 ms
84,972 KB |
testcase_17 | AC | 312 ms
102,936 KB |
testcase_18 | AC | 186 ms
85,156 KB |
testcase_19 | AC | 451 ms
101,480 KB |
testcase_20 | AC | 428 ms
101,348 KB |
testcase_21 | AC | 412 ms
101,292 KB |
testcase_22 | AC | 296 ms
96,384 KB |
testcase_23 | AC | 302 ms
97,128 KB |
testcase_24 | AC | 301 ms
97,760 KB |
testcase_25 | AC | 450 ms
101,588 KB |
testcase_26 | AC | 413 ms
101,424 KB |
testcase_27 | AC | 410 ms
101,704 KB |
testcase_28 | AC | 441 ms
101,340 KB |
testcase_29 | AC | 395 ms
100,988 KB |
testcase_30 | AC | 406 ms
101,688 KB |
testcase_31 | AC | 444 ms
101,440 KB |
ソースコード
# boxの中身を全部移動するのではなく、boxとboxをつなげるUnion Findでどうか # 既にuniteならつなげない 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 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()) from collections import defaultdict color = defaultdict(list) for i in range(N): b, c = map(int, input().split()) color[c].append(b) #print(color) UF = UnionFind(M+1) ans = 0 for c in color: if len(color[c]) > 1: base = color[c][0] for j in range(1, len(color[c])): if UF.same(base, color[c][j]) == False: UF.union(base, color[c][j]) ans += 1 print(ans)