結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-05-16 22:39:41
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 868 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 81,464 KB
実行使用メモリ 260,508 KB
最終ジャッジ日時 2023-10-24 04:30:57
合計ジャッジ時間 7,104 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,444 KB
testcase_01 AC 36 ms
53,444 KB
testcase_02 AC 36 ms
53,444 KB
testcase_03 AC 769 ms
93,728 KB
testcase_04 AC 576 ms
96,592 KB
testcase_05 AC 679 ms
94,304 KB
testcase_06 AC 706 ms
175,556 KB
testcase_07 RE -
testcase_08 AC 856 ms
208,368 KB
testcase_09 AC 425 ms
100,256 KB
testcase_10 AC 295 ms
83,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit


def find(parent, amount, i):
    t = parent[i]
    if t < 0:
        return i, 0
    t, a = find(parent, amount, t)
    parent[i] = t
    amount[i] += a
    return t, amount[i]


def unite(parent, amount, i, j):
    i, _ = find(parent, amount, i)
    j, _ = find(parent, amount, j)
    if i == j:
        return
    parent[j] += parent[i]
    parent[i] = j
    amount[i] -= amount[j]


setrecursionlimit(10 ** 5)

N, Q = map(int, input().split())

parent = [-1] * (N + 1)
amount = [0] * (N + 1)

result = []
for _ in range(Q):
    T, A, B = map(int, input().split())

    if T == 1:
        unite(parent, amount, A, B)
    elif T == 2:
        j, _ = find(parent, amount, A)
        amount[j] += B
    elif T == 3:
        j, a = find(parent, amount, A)
        result.append(amount[j] + a)
print('\n'.join(str(v) for v in result))
0