結果
問題 | No.1054 Union add query |
ユーザー | 37zigen |
提出日時 | 2019-11-09 04:23:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 758 ms / 2,000 ms |
コード長 | 1,480 bytes |
コンパイル時間 | 1,302 ms |
コンパイル使用メモリ | 86,628 KB |
実行使用メモリ | 88,344 KB |
最終ジャッジ日時 | 2023-08-18 17:18:21 |
合計ジャッジ時間 | 6,101 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 81 ms
71,204 KB |
testcase_01 | AC | 83 ms
71,200 KB |
testcase_02 | AC | 81 ms
71,312 KB |
testcase_03 | AC | 686 ms
85,904 KB |
testcase_04 | AC | 486 ms
88,344 KB |
testcase_05 | AC | 758 ms
87,064 KB |
testcase_06 | AC | 261 ms
80,992 KB |
testcase_07 | AC | 213 ms
81,044 KB |
testcase_08 | AC | 245 ms
80,924 KB |
testcase_09 | AC | 310 ms
86,188 KB |
testcase_10 | AC | 167 ms
84,396 KB |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines class DJSet(): def __init__(self, n): self.upper = [-1]*n self.weight = [0]*n def equiv(self, x, y): return self.root(x) == self.root(y) def root(self, x): if self.upper[x] < 0: return x else: return self.root(self.upper[x]) def get_weight(self, x): if self.upper[x] < 0: return self.weight[x] else: return self.weight[x] + self.get_weight(self.upper[x]) def setUnion(self, x, y): x = self.root(x) y = self.root(y) if x == y: return if self.upper[x] < self.upper[y]: x ^= y y ^= x x ^= y self.upper[y] = self.upper[y] + self.upper[x] self.upper[x] = y self.weight[x] = self.weight[x] - self.weight[y] def add_weight(self, x, w): x = self.root(x) self.weight[x] = self.weight[x] + w def run(): N, Q = map(int, readline().split()) ds = DJSet(N) for q in range(Q): T, A, B = map(int, readline().split()) A = A - 1 if (T == 1): B = B - 1 if (ds.equiv(A, B)): continue ds.setUnion(A, B) elif (T == 2): ds.add_weight(A, B) elif (T == 3): print(ds.get_weight(A)) run()