結果
問題 | No.1293 2種類の道路 |
ユーザー | 👑 rin204 |
提出日時 | 2022-11-11 16:09:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 992 ms / 2,000 ms |
コード長 | 2,435 bytes |
コンパイル時間 | 508 ms |
コンパイル使用メモリ | 86,776 KB |
実行使用メモリ | 133,968 KB |
最終ジャッジ日時 | 2023-10-11 12:54:46 |
合計ジャッジ時間 | 11,909 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
71,356 KB |
testcase_01 | AC | 72 ms
71,276 KB |
testcase_02 | AC | 72 ms
71,568 KB |
testcase_03 | AC | 71 ms
71,256 KB |
testcase_04 | AC | 73 ms
71,548 KB |
testcase_05 | AC | 73 ms
71,224 KB |
testcase_06 | AC | 71 ms
71,504 KB |
testcase_07 | AC | 72 ms
71,232 KB |
testcase_08 | AC | 75 ms
71,072 KB |
testcase_09 | AC | 992 ms
132,180 KB |
testcase_10 | AC | 957 ms
132,040 KB |
testcase_11 | AC | 979 ms
131,276 KB |
testcase_12 | AC | 874 ms
133,968 KB |
testcase_13 | AC | 911 ms
132,516 KB |
testcase_14 | AC | 572 ms
112,800 KB |
testcase_15 | AC | 550 ms
114,300 KB |
testcase_16 | AC | 393 ms
117,160 KB |
testcase_17 | AC | 411 ms
123,552 KB |
testcase_18 | AC | 323 ms
106,552 KB |
testcase_19 | AC | 621 ms
122,820 KB |
testcase_20 | AC | 579 ms
125,112 KB |
testcase_21 | AC | 441 ms
103,176 KB |
testcase_22 | AC | 448 ms
102,348 KB |
testcase_23 | AC | 300 ms
99,768 KB |
ソースコード
class RollbackUnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n self.group = n self.history = [] self.inner_snap = 0 def find(self, x): if self.parents[x] < 0: return x else: return self.find(self.parents[x]) def union(self, x, y): x = self.find(x) y = self.find(y) self.history.append((x, self.parents[x])) self.history.append((y, self.parents[y])) if x == y: return False self.group -= 1 if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x return True 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 self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) def undo(self): x, p = self.history.pop() self.parents[x] = p y, p = self.history.pop() self.parents[y] = p if x != y: self.group += 1 def snapshot(self): self.inner_snap = len(self.history) >> 1 def get_state(self): return len(self.history) >> 1 def rollback(self, state=-1): if state == -1: state = self.inner_snap state <<= 1 assert state <= len(self.history) while state < len(self.history): self.undo() n, d, w = map(int, input().split()) UF1 = RollbackUnionFind(n) UF2 = RollbackUnionFind(n) for _ in range(d): a, b = map(int, input().split()) UF1.union(a - 1, b - 1) for _ in range(w): a, b = map(int, input().split()) UF2.union(a - 1, b - 1) ans = -n for group in UF1.all_group_members().values(): u = group[0] UF2.snapshot() for v in group[1:]: UF2.union(u, v) ans += UF1.size(u) * UF2.size(u) UF2.rollback() print(ans)