結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,064 KB
testcase_01 AC 28 ms
10,064 KB
testcase_02 AC 28 ms
10,064 KB
testcase_03 AC 1,484 ms
32,488 KB
testcase_04 AC 1,200 ms
41,200 KB
testcase_05 AC 1,374 ms
31,016 KB
testcase_06 AC 1,062 ms
32,016 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 1,037 ms
45,508 KB
testcase_10 AC 786 ms
17,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
from sys import stdin


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)
readline = stdin.readline

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

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

result = []
for _ in range(Q):
    T, A, B = map(int, readline().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