結果
問題 | No.2295 Union Path Query (Medium) |
ユーザー | t98slider |
提出日時 | 2023-03-16 04:50:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 352 ms / 4,000 ms |
コード長 | 1,468 bytes |
コンパイル時間 | 649 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 84,608 KB |
最終ジャッジ日時 | 2024-05-02 03:27:49 |
合計ジャッジ時間 | 21,283 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
52,352 KB |
testcase_01 | AC | 38 ms
51,968 KB |
testcase_02 | AC | 37 ms
52,096 KB |
testcase_03 | AC | 334 ms
83,712 KB |
testcase_04 | AC | 322 ms
83,584 KB |
testcase_05 | AC | 277 ms
82,048 KB |
testcase_06 | AC | 239 ms
82,688 KB |
testcase_07 | AC | 311 ms
80,128 KB |
testcase_08 | AC | 352 ms
83,200 KB |
testcase_09 | AC | 350 ms
83,400 KB |
testcase_10 | AC | 330 ms
83,328 KB |
testcase_11 | AC | 329 ms
82,944 KB |
testcase_12 | AC | 343 ms
83,456 KB |
testcase_13 | AC | 346 ms
83,828 KB |
testcase_14 | AC | 263 ms
84,224 KB |
testcase_15 | AC | 196 ms
81,024 KB |
testcase_16 | AC | 199 ms
80,896 KB |
testcase_17 | AC | 266 ms
83,200 KB |
testcase_18 | AC | 328 ms
83,456 KB |
testcase_19 | AC | 307 ms
82,816 KB |
testcase_20 | AC | 305 ms
83,200 KB |
testcase_21 | AC | 290 ms
82,944 KB |
testcase_22 | AC | 306 ms
83,072 KB |
testcase_23 | AC | 268 ms
84,608 KB |
testcase_24 | AC | 270 ms
82,304 KB |
testcase_25 | AC | 229 ms
81,408 KB |
testcase_26 | AC | 275 ms
82,560 KB |
testcase_27 | AC | 256 ms
81,792 KB |
testcase_28 | AC | 270 ms
82,048 KB |
testcase_29 | AC | 262 ms
81,928 KB |
testcase_30 | AC | 262 ms
82,432 KB |
testcase_31 | AC | 264 ms
81,792 KB |
testcase_32 | AC | 284 ms
81,920 KB |
testcase_33 | AC | 263 ms
81,920 KB |
testcase_34 | AC | 257 ms
81,956 KB |
testcase_35 | AC | 280 ms
81,924 KB |
testcase_36 | AC | 276 ms
81,792 KB |
testcase_37 | AC | 294 ms
82,172 KB |
testcase_38 | AC | 269 ms
81,824 KB |
testcase_39 | AC | 279 ms
81,792 KB |
testcase_40 | AC | 261 ms
82,180 KB |
testcase_41 | AC | 262 ms
81,920 KB |
testcase_42 | AC | 267 ms
81,792 KB |
testcase_43 | AC | 326 ms
82,044 KB |
testcase_44 | AC | 266 ms
82,048 KB |
testcase_45 | AC | 270 ms
81,968 KB |
testcase_46 | AC | 314 ms
82,688 KB |
testcase_47 | AC | 296 ms
82,560 KB |
testcase_48 | AC | 342 ms
82,944 KB |
testcase_49 | AC | 331 ms
83,200 KB |
testcase_50 | AC | 259 ms
82,560 KB |
testcase_51 | AC | 254 ms
82,432 KB |
testcase_52 | AC | 259 ms
82,304 KB |
testcase_53 | AC | 273 ms
82,432 KB |
testcase_54 | AC | 276 ms
82,048 KB |
testcase_55 | AC | 270 ms
82,432 KB |
ソースコード
from sys import stdin class UnionFind(): def __init__(self, n): self.parent_or_size = [-1] * n self.weight = [1 << 30] * n self.ans = [0] * n def leader(self, x): while self.parent_or_size[x] >= 0: x = self.parent_or_size[x] return x def merge(self, x, y, w): rx, ry = self.leader(x), self.leader(y) if rx == ry: return if self.parent_or_size[rx] > self.parent_or_size[ry]: rx, ry = ry, rx self.ans[rx] += self.ans[ry] + w * self.parent_or_size[rx] * self.parent_or_size[ry] self.ans[rx] %= 998244353 self.parent_or_size[rx] += self.parent_or_size[ry] self.parent_or_size[ry] = rx self.weight[ry] = w def dist(self, x, y): if self.leader(x) != self.leader(y): return -1 ans = 0 while x != y: if self.weight[x] > self.weight[y]: x, y = y, x ans = self.weight[x] x = self.parent_or_size[x] return ans N, X, Q = map(int, stdin.readline().split()) uf = UnionFind(N) for _ in range(Q): query = list(map(int, stdin.readline().split())) if query[0] == 1: uf.merge(X, query[1], query[2]) elif query[0] == 2: d = uf.dist(query[1], query[2]) print(d) if d != -1: X += d X %= N elif query[0] == 3: print(uf.ans[uf.leader(query[1])]) else: X += query[1] if X >= N: X -= N