結果
問題 | No.2290 UnUnion Find |
ユーザー | kuro_B |
提出日時 | 2023-05-09 17:58:22 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,072 bytes |
コンパイル時間 | 370 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 142,592 KB |
最終ジャッジ日時 | 2024-11-26 03:43:34 |
合計ジャッジ時間 | 22,391 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 124 ms
82,944 KB |
testcase_01 | WA | - |
testcase_02 | AC | 267 ms
109,824 KB |
testcase_03 | AC | 304 ms
125,056 KB |
testcase_04 | AC | 276 ms
124,800 KB |
testcase_05 | AC | 295 ms
124,800 KB |
testcase_06 | AC | 279 ms
124,928 KB |
testcase_07 | AC | 294 ms
125,056 KB |
testcase_08 | AC | 312 ms
124,800 KB |
testcase_09 | AC | 286 ms
125,056 KB |
testcase_10 | AC | 282 ms
124,800 KB |
testcase_11 | AC | 283 ms
124,800 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 287 ms
124,800 KB |
testcase_15 | AC | 288 ms
124,800 KB |
testcase_16 | AC | 285 ms
124,672 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 409 ms
133,888 KB |
testcase_25 | WA | - |
testcase_26 | AC | 396 ms
136,832 KB |
testcase_27 | AC | 445 ms
133,888 KB |
testcase_28 | WA | - |
testcase_29 | AC | 451 ms
131,200 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 432 ms
122,112 KB |
testcase_34 | AC | 437 ms
142,464 KB |
testcase_35 | AC | 478 ms
136,832 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | AC | 461 ms
133,888 KB |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | AC | 278 ms
124,544 KB |
testcase_45 | AC | 306 ms
124,672 KB |
testcase_46 | WA | - |
ソースコード
###スニペット始まり### 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(q[2]-1) else: parents.discard(q[1]-1) else: if len(parents)==1: print(-1) else: for p in parents: if p!=q[1]-1: print(p+1) break