結果
問題 | No.1390 Get together |
ユーザー | 👑 H20 |
提出日時 | 2021-04-26 01:31:18 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,576 bytes |
コンパイル時間 | 259 ms |
コンパイル使用メモリ | 86,756 KB |
実行使用メモリ | 100,600 KB |
最終ジャッジ日時 | 2023-09-17 14:31:14 |
合計ジャッジ時間 | 11,949 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 92 ms
71,324 KB |
testcase_01 | AC | 93 ms
71,388 KB |
testcase_02 | AC | 94 ms
71,268 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | AC | 339 ms
96,568 KB |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | AC | 417 ms
97,072 KB |
testcase_21 | AC | 463 ms
98,648 KB |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | AC | 410 ms
97,460 KB |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | AC | 406 ms
97,612 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 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()) L = [[] for i in range(N+1)] for i in range(N): b,c = map(int, input().split()) L[c].append(b) uf = UnionFind(N) cnt = 0 for l in L: for c in l: if not uf.same(l[0],c): uf.union(l[0],c) cnt+=1 print(cnt)