結果
問題 | No.1054 Union add query |
ユーザー | sgsw |
提出日時 | 2021-05-23 03:03:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,488 ms / 2,000 ms |
コード長 | 3,665 bytes |
コンパイル時間 | 436 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 302,996 KB |
最終ジャッジ日時 | 2024-10-11 02:54:40 |
合計ジャッジ時間 | 9,876 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 69 ms
66,688 KB |
testcase_01 | AC | 63 ms
66,816 KB |
testcase_02 | AC | 61 ms
66,688 KB |
testcase_03 | AC | 1,007 ms
183,900 KB |
testcase_04 | AC | 1,488 ms
302,996 KB |
testcase_05 | AC | 891 ms
160,448 KB |
testcase_06 | AC | 693 ms
220,456 KB |
testcase_07 | AC | 612 ms
224,488 KB |
testcase_08 | AC | 644 ms
222,628 KB |
testcase_09 | AC | 1,026 ms
297,472 KB |
testcase_10 | AC | 743 ms
296,704 KB |
ソースコード
import types _atcoder_code = """ # Python port of AtCoder Library. __all__ = ["string","lazysegtree","convolution","maxflow","modint" ,"mincostflow","segtree","_scc","_math","math","dsu","twosat","fenwicktree","scc","_bit","lca","unverified"] __version__ = '0.0.1' """ atcoder = types.ModuleType('atcoder') exec(_atcoder_code, atcoder.__dict__) _atcoder_dsu_code = """ import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= b < self._n x = self.leader(a) y = self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) """ atcoder.dsu = types.ModuleType('atcoder.dsu') exec(_atcoder_dsu_code, atcoder.dsu.__dict__) DSU = atcoder.dsu.DSU from collections import defaultdict # from atcoder.dsu import DSU import sys #Library Info(ACL for Python/Pypy) -> https://github.com/not522/ac-library-python def input(): return sys.stdin.readline().rstrip() def main(): n, q = map(int, input().split()) Query = [tuple(map(int, input().split())) for i in range(q)] add_val = [0]*(n) dsu = DSU(n) Group = [defaultdict(int) for i in range(n)] for i in range(n): Group[i][i] = 0 for t, a, b in Query: if t == 1: a -= 1 b -= 1 if dsu.same(a, b): continue par_a, par_b = dsu.leader(a), dsu.leader(b) dsu.merge(a, b) par_ab = dsu.leader(a) if par_ab == par_a: #aの方がサイズがでかい #a <- bマージ for key, val in Group[par_b].items(): Group[par_a][key] = val + add_val[par_b] - add_val[par_a] else: for key, val in Group[par_a].items(): Group[par_b][key] = val + add_val[par_a] - add_val[par_b] elif t == 2: a -= 1 add_val[dsu.leader(a)] += b else: a -= 1 par_a = dsu.leader(a) res = Group[par_a][a] + add_val[par_a] print(res) return 0 if __name__ == "__main__": main()