結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-05-16 22:38:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 868 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 11,956 KB
実行使用メモリ 47,968 KB
最終ジャッジ日時 2023-10-24 04:29:44
合計ジャッジ時間 15,709 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,124 KB
testcase_01 AC 28 ms
10,124 KB
testcase_02 AC 27 ms
10,124 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,878 ms
32,076 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 1,883 ms
45,568 KB
testcase_10 AC 1,588 ms
17,924 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