結果
問題 | No.1293 2種類の道路 |
ユーザー | 👑 rin204 |
提出日時 | 2022-11-11 16:09:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 916 ms / 2,000 ms |
コード長 | 2,435 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 82,028 KB |
実行使用メモリ | 127,040 KB |
最終ジャッジ日時 | 2024-09-13 11:41:42 |
合計ジャッジ時間 | 10,465 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,168 KB |
testcase_01 | AC | 39 ms
52,744 KB |
testcase_02 | AC | 39 ms
53,852 KB |
testcase_03 | AC | 40 ms
54,696 KB |
testcase_04 | AC | 41 ms
53,668 KB |
testcase_05 | AC | 40 ms
54,108 KB |
testcase_06 | AC | 39 ms
52,812 KB |
testcase_07 | AC | 39 ms
54,532 KB |
testcase_08 | AC | 42 ms
54,712 KB |
testcase_09 | AC | 913 ms
126,032 KB |
testcase_10 | AC | 901 ms
126,440 KB |
testcase_11 | AC | 916 ms
127,040 KB |
testcase_12 | AC | 838 ms
123,632 KB |
testcase_13 | AC | 859 ms
123,312 KB |
testcase_14 | AC | 540 ms
108,916 KB |
testcase_15 | AC | 502 ms
108,292 KB |
testcase_16 | AC | 374 ms
115,472 KB |
testcase_17 | AC | 379 ms
117,336 KB |
testcase_18 | AC | 295 ms
106,580 KB |
testcase_19 | AC | 565 ms
124,228 KB |
testcase_20 | AC | 548 ms
123,592 KB |
testcase_21 | AC | 412 ms
99,800 KB |
testcase_22 | AC | 422 ms
100,396 KB |
testcase_23 | AC | 280 ms
97,252 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)