結果
問題 | No.2290 UnUnion Find |
ユーザー | AEn |
提出日時 | 2023-07-26 00:20:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 606 ms / 2,000 ms |
コード長 | 1,655 bytes |
コンパイル時間 | 302 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 143,920 KB |
最終ジャッジ日時 | 2024-04-10 15:15:03 |
合計ジャッジ時間 | 21,729 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
52,352 KB |
testcase_01 | AC | 39 ms
52,352 KB |
testcase_02 | AC | 124 ms
77,004 KB |
testcase_03 | AC | 225 ms
107,032 KB |
testcase_04 | AC | 237 ms
107,136 KB |
testcase_05 | AC | 247 ms
107,136 KB |
testcase_06 | AC | 239 ms
107,100 KB |
testcase_07 | AC | 248 ms
107,136 KB |
testcase_08 | AC | 244 ms
107,032 KB |
testcase_09 | AC | 251 ms
107,360 KB |
testcase_10 | AC | 241 ms
107,264 KB |
testcase_11 | AC | 248 ms
107,008 KB |
testcase_12 | AC | 241 ms
107,124 KB |
testcase_13 | AC | 254 ms
107,136 KB |
testcase_14 | AC | 265 ms
107,236 KB |
testcase_15 | AC | 258 ms
107,136 KB |
testcase_16 | AC | 216 ms
107,520 KB |
testcase_17 | AC | 205 ms
107,136 KB |
testcase_18 | AC | 254 ms
107,500 KB |
testcase_19 | AC | 606 ms
111,820 KB |
testcase_20 | AC | 479 ms
143,772 KB |
testcase_21 | AC | 293 ms
80,756 KB |
testcase_22 | AC | 465 ms
91,904 KB |
testcase_23 | AC | 524 ms
91,904 KB |
testcase_24 | AC | 511 ms
126,208 KB |
testcase_25 | AC | 392 ms
86,596 KB |
testcase_26 | AC | 472 ms
132,512 KB |
testcase_27 | AC | 564 ms
126,060 KB |
testcase_28 | AC | 512 ms
104,192 KB |
testcase_29 | AC | 575 ms
120,448 KB |
testcase_30 | AC | 357 ms
81,408 KB |
testcase_31 | AC | 546 ms
116,096 KB |
testcase_32 | AC | 402 ms
86,868 KB |
testcase_33 | AC | 515 ms
101,364 KB |
testcase_34 | AC | 463 ms
143,920 KB |
testcase_35 | AC | 563 ms
132,352 KB |
testcase_36 | AC | 435 ms
88,320 KB |
testcase_37 | AC | 563 ms
98,560 KB |
testcase_38 | AC | 391 ms
88,064 KB |
testcase_39 | AC | 401 ms
93,568 KB |
testcase_40 | AC | 526 ms
126,720 KB |
testcase_41 | AC | 436 ms
85,220 KB |
testcase_42 | AC | 510 ms
111,232 KB |
testcase_43 | AC | 565 ms
107,776 KB |
testcase_44 | AC | 209 ms
107,212 KB |
testcase_45 | AC | 227 ms
107,720 KB |
testcase_46 | AC | 227 ms
107,792 KB |
ソースコード
class UnionFind: """0-indexed""" def __init__(self, n): self.n = n self.parent = [-1] * n self.__group_count = n def unite(self, x, y) -> bool: """xとyをマージ""" x = self.root(x) y = self.root(y) if x == y: return False self.__group_count -= 1 if self.parent[x] > self.parent[y]: x, y = y, x self.parent[x] += self.parent[y] self.parent[y] = x return True def is_same(self, x, y) -> bool: """xとyが同じ連結成分か判定""" return self.root(x) == self.root(y) def root(self, x) -> int: """xの根を取得""" if self.parent[x] < 0: return x else: # 経路圧縮あり # self.parent[x] = self.root(self.parent[x]) # return self.parent[x] # 経路圧縮なし return self.root(self.parent[x]) import sys input = sys.stdin.readline N,Q = map(int, input().split()) d = {} s = set() uf = UnionFind(N) for i in range(N): d[uf.root(i)] = i s.add(i) for i in range(Q): q = list(map(int, input().split())) if q[0]==1: a,b = q[1]-1,q[2]-1 ra,rb = uf.root(a),uf.root(b) if ra==rb:continue uf.unite(a,b) rab = uf.root(a) if ra==rab: s.remove(d[rb]) else: s.remove(d[ra]) else: a = q[1]-1 if len(s)==1: print(-1) else: r = uf.root(a) for s_ in s: if s_!=d[r]: print(s_+1) break