結果

問題 No.1054 Union add query
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-18 19:56:12
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 943 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 104,548 KB
最終ジャッジ日時 2023-10-21 08:14:27
合計ジャッジ時間 4,309 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,468 KB
testcase_01 AC 39 ms
53,468 KB
testcase_02 AC 35 ms
53,468 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
tmp = [0] * N
num = [0] * N

root = [[i] for i in range(N)]


def find(x):
    if isinstance(root[x], list):
        return x
    res = find(root[x])
    root[x] = res
    return res


def merge(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if len(root[x]) > len(root[y]):
        x, y = y, x
    
    tx = tmp[x]
    if tx:
        for i in root[x]:
            num[i] += tx
        tmp[x] = 0
    
    ty = tmp[y]
    if ty:
        for i in root[y]:
            num[i] += ty
        tmp[y] = 0

    root[y].extend(root[x])
    root[x] = y


    
ans = []
for _ in range(Q):
    t, a, b = map(int, input().split())
    if t == 1:
        merge(a-1, b-1)
    elif t == 2:
        leader = find(a-1)
        tmp[leader] += b
    else:
        leader = find(a-1)
        ans.append(num[a-1] + tmp[leader])

        
print(*ans, sep="\n")
0