結果
問題 | No.1054 Union add query |
ユーザー | toyuzuko |
提出日時 | 2020-05-28 21:16:35 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,282 bytes |
コンパイル時間 | 107 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 57,984 KB |
最終ジャッジ日時 | 2024-04-21 10:56:38 |
合計ジャッジ時間 | 11,421 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 25 ms
10,752 KB |
testcase_01 | AC | 25 ms
10,752 KB |
testcase_02 | AC | 26 ms
10,752 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 961 ms
28,988 KB |
testcase_07 | AC | 898 ms
28,920 KB |
testcase_08 | AC | 945 ms
21,592 KB |
testcase_09 | AC | 1,150 ms
57,984 KB |
testcase_10 | AC | 820 ms
34,304 KB |
ソースコード
class UnionFind(): def __init__(self, n): self.n = n self.parents = [i for i in range(n)] self.added = [0 for _ in range(n)] def find(self, x): root = x while self.parents[root] != root: root = self.parents[root] while self.parents[x] != root: parent = self.parents[x] self.parents[x] = root x = parent return root def unite(self, x, y): xroot = self.find(x) yroot = self.find(y) if xroot == yroot: return False self.parents[xroot] = yroot self.added[xroot] -= self.added[yroot] return True def add(self, x, a): self.added[self.find(x)] += a def get(self, x): res = 0 root = x while self.parents[root] != root: res += self.added[root] root = self.parents[root] res += self.added[root] return res import sys input = sys.stdin.readline N, Q = map(int, input().split()) uf = UnionFind(N) res = [] for _ in range(Q): t, a, b = map(int, input().split()) if t == 1: uf.unite(a - 1, b - 1) elif t == 2: uf.add(a - 1, b) else: res.append(uf.get(a - 1)) print('\n'.join(map(str, res)))