結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
toyuzuko
|
| 提出日時 | 2020-05-28 21:16:35 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,282 bytes |
| コンパイル時間 | 128 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 57,856 KB |
| 最終ジャッジ日時 | 2024-10-13 09:41:34 |
| 合計ジャッジ時間 | 12,329 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 5 WA * 3 |
ソースコード
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)))
toyuzuko