結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
brthyyjp
|
| 提出日時 | 2020-05-26 04:27:50 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 785 ms / 2,000 ms |
| コード長 | 1,091 bytes |
| コンパイル時間 | 229 ms |
| コンパイル使用メモリ | 82,816 KB |
| 実行使用メモリ | 233,080 KB |
| 最終ジャッジ日時 | 2024-10-13 02:32:33 |
| 合計ジャッジ時間 | 6,368 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
def Find(x, par):
if par[x] < 0:
return x
else:
par[x] = Find(par[x], par)
return par[x]
def Unite(x, y, par, size, plus, child):
x = Find(x, par)
y = Find(y, par)
if x != y:
if size[x] < size[y]:
x, y = y, x
size[x] += size[y]
par[y] = x
sub = plus[y]-plus[x]
plus[y] = 0
for k in child[y]:
child[x].add(k)
plus[k] += sub
child[y] = set()
def Same(x, y, par):
return Find(x, par) == Find(y, par)
def Size(x, par, size):
return size[Find(x, par)]
import sys
input = sys.stdin.buffer.readline
n, q = map(int, input().split())
par = [-1]*n
size = [1]*n
child = [{i} for i in range(n)]
plus = [0]*n
for i in range(q):
t, a, b = map(int, input().split())
if t == 1:
a, b = a-1, b-1
Unite(a, b, par, size, plus, child)
elif t == 2:
a -= 1
plus[Find(a, par)] += b
else:
a -= 1
if par[a] == -1:
print(plus[a])
else:
print(plus[a]+plus[Find(a, par)])
brthyyjp