結果
問題 | No.2290 UnUnion Find |
ユーザー | kuro_B |
提出日時 | 2023-05-09 18:01:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 587 ms / 2,000 ms |
コード長 | 3,071 bytes |
コンパイル時間 | 407 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 142,464 KB |
最終ジャッジ日時 | 2024-11-26 03:45:33 |
合計ジャッジ時間 | 25,900 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 133 ms
82,816 KB |
testcase_01 | AC | 136 ms
82,816 KB |
testcase_02 | AC | 280 ms
110,080 KB |
testcase_03 | AC | 325 ms
125,056 KB |
testcase_04 | AC | 332 ms
124,928 KB |
testcase_05 | AC | 339 ms
124,672 KB |
testcase_06 | AC | 329 ms
125,056 KB |
testcase_07 | AC | 354 ms
124,800 KB |
testcase_08 | AC | 342 ms
124,928 KB |
testcase_09 | AC | 340 ms
124,672 KB |
testcase_10 | AC | 340 ms
124,800 KB |
testcase_11 | AC | 360 ms
124,800 KB |
testcase_12 | AC | 358 ms
124,928 KB |
testcase_13 | AC | 354 ms
124,800 KB |
testcase_14 | AC | 339 ms
124,928 KB |
testcase_15 | AC | 342 ms
125,056 KB |
testcase_16 | AC | 326 ms
124,928 KB |
testcase_17 | AC | 335 ms
124,928 KB |
testcase_18 | AC | 338 ms
124,928 KB |
testcase_19 | AC | 587 ms
126,976 KB |
testcase_20 | AC | 541 ms
142,464 KB |
testcase_21 | AC | 407 ms
111,744 KB |
testcase_22 | AC | 489 ms
117,888 KB |
testcase_23 | AC | 509 ms
117,888 KB |
testcase_24 | AC | 497 ms
133,760 KB |
testcase_25 | AC | 485 ms
115,328 KB |
testcase_26 | AC | 487 ms
136,960 KB |
testcase_27 | AC | 534 ms
133,888 KB |
testcase_28 | AC | 508 ms
123,648 KB |
testcase_29 | AC | 544 ms
131,200 KB |
testcase_30 | AC | 448 ms
113,280 KB |
testcase_31 | AC | 519 ms
129,024 KB |
testcase_32 | AC | 462 ms
115,328 KB |
testcase_33 | AC | 522 ms
122,496 KB |
testcase_34 | AC | 522 ms
142,464 KB |
testcase_35 | AC | 569 ms
136,960 KB |
testcase_36 | AC | 459 ms
115,968 KB |
testcase_37 | AC | 543 ms
121,216 KB |
testcase_38 | AC | 462 ms
115,968 KB |
testcase_39 | AC | 494 ms
118,656 KB |
testcase_40 | AC | 572 ms
134,016 KB |
testcase_41 | AC | 509 ms
115,200 KB |
testcase_42 | AC | 567 ms
126,848 KB |
testcase_43 | AC | 581 ms
124,928 KB |
testcase_44 | AC | 340 ms
125,312 KB |
testcase_45 | AC | 357 ms
125,056 KB |
testcase_46 | AC | 379 ms
124,928 KB |
ソースコード
###スニペット始まり### import sys, re from copy import copy, deepcopy from math import ceil, floor, sqrt,factorial, gcd, pi, degrees, radians, sin, asin, cos, acos, tan, atan2 from collections import Counter, deque, defaultdict from heapq import heapify, heappop, heappush from itertools import permutations, accumulate, product, combinations, combinations_with_replacement from bisect import bisect, bisect_left, bisect_right from functools import reduce, lru_cache from string import ascii_uppercase, ascii_lowercase from decimal import Decimal, ROUND_HALF_UP #四捨五入用 def input(): return sys.stdin.readline().rstrip('\n') #easy-testのpypyでは再帰数を下げる。 if __file__=='prog.py': sys.setrecursionlimit(10**5) else: sys.setrecursionlimit(10**6) def lcm(a, b): return a * b // gcd(a, b) #3つ以上の最大公約数/最小公倍数。Nを要素数、Mを数値の大きさとして、O(NlogM) def gcd_v2(l: list): return reduce(gcd, l) def lcm_v2(l: list): return reduce(lcm, l) #nPk def nPk(n, k): return factorial(n) // factorial(n - k) #逆元 def modinv(a, mod=10**9+7): return pow(a, mod-2, mod) INF = float('inf') MOD = 10 ** 9 + 7 ###スニペット終わり### #0-index class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N, Q=map(int, input().split()) query=[list(map(int, input().split())) for _ in range(Q)] uf=UnionFind(N) parents=set(range(N)) for q in query: if q[0]==1: x=uf.find(q[1]-1) y=uf.find(q[2]-1) if x!=y: uf.union(q[1]-1, q[2]-1) if x==uf.find(q[1]-1): parents.discard(y) else: parents.discard(x) else: if len(parents)==1: print(-1) else: for p in parents: if p!=uf.find(q[1]-1): print(p+1) break