結果
問題 | No.1054 Union add query |
ユーザー | tktk_snsn |
提出日時 | 2021-02-03 23:21:48 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 955 bytes |
コンパイル時間 | 945 ms |
コンパイル使用メモリ | 87,076 KB |
実行使用メモリ | 71,492 KB |
最終ジャッジ日時 | 2023-09-12 18:58:13 |
合計ジャッジ時間 | 6,721 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 70 ms
71,492 KB |
testcase_01 | AC | 71 ms
71,324 KB |
testcase_02 | AC | 71 ms
71,392 KB |
testcase_03 | TLE | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
ソースコード
import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N, Q = map(int, input().split()) query = tuple(tuple(map(int, input().split())) for _ in range(Q)) root = [-1] * N member = [[i] for i in range(N)] leader_val = [0] * N each_val = [0] * N def find(x): if root[x] < 0: return x root[x] = res = find(root[x]) return res def unite(x, y): x = find(x) y = find(y) if x == y: return # x:small, y:big if -root[x] > -root[y]: x, y = y, x xval = leader_val[x] yval = leader_val[y] leader_val[y] = 0 root[x] += root[y] root[y] = x while member[y]: i = member[y].pop() each_val[i] += yval - xval member[x].append(i) for t, a, b in query: if t == 1: unite(a - 1, b - 1) elif t == 2: x = find(a - 1) leader_val[x] += b else: x = find(a - 1) print(leader_val[x] + each_val[a-1])