結果
問題 | No.1293 2種類の道路 |
ユーザー | 👑 rin204 |
提出日時 | 2022-11-11 16:06:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 910 ms / 2,000 ms |
コード長 | 2,445 bytes |
コンパイル時間 | 550 ms |
コンパイル使用メモリ | 81,588 KB |
実行使用メモリ | 127,188 KB |
最終ジャッジ日時 | 2024-09-13 11:39:35 |
合計ジャッジ時間 | 11,633 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,736 KB |
testcase_01 | AC | 39 ms
52,608 KB |
testcase_02 | AC | 40 ms
52,224 KB |
testcase_03 | AC | 39 ms
52,480 KB |
testcase_04 | AC | 42 ms
52,992 KB |
testcase_05 | AC | 41 ms
52,480 KB |
testcase_06 | AC | 39 ms
52,480 KB |
testcase_07 | AC | 39 ms
52,736 KB |
testcase_08 | AC | 41 ms
53,376 KB |
testcase_09 | AC | 898 ms
125,964 KB |
testcase_10 | AC | 889 ms
126,320 KB |
testcase_11 | AC | 910 ms
127,188 KB |
testcase_12 | AC | 827 ms
123,508 KB |
testcase_13 | AC | 853 ms
123,944 KB |
testcase_14 | AC | 536 ms
108,764 KB |
testcase_15 | AC | 500 ms
107,032 KB |
testcase_16 | AC | 368 ms
115,400 KB |
testcase_17 | AC | 376 ms
117,596 KB |
testcase_18 | AC | 295 ms
106,748 KB |
testcase_19 | AC | 558 ms
123,972 KB |
testcase_20 | AC | 555 ms
123,508 KB |
testcase_21 | AC | 413 ms
99,444 KB |
testcase_22 | AC | 422 ms
100,140 KB |
testcase_23 | AC | 283 ms
97,376 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] ver = UF2.get_state() for v in group[1:]: UF2.union(u, v) ans += UF1.size(u) * UF2.size(u) UF2.rollback(ver) print(ans)