結果

問題 No.2290 UnUnion Find
ユーザー kuro_Bkuro_B
提出日時 2023-05-09 18:01:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 3,071 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,148 KB
実行使用メモリ 142,592 KB
最終ジャッジ日時 2024-05-04 15:30:17
合計ジャッジ時間 21,559 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
82,432 KB
testcase_01 AC 111 ms
82,432 KB
testcase_02 AC 227 ms
110,336 KB
testcase_03 AC 260 ms
125,184 KB
testcase_04 AC 259 ms
125,056 KB
testcase_05 AC 268 ms
125,172 KB
testcase_06 AC 257 ms
125,036 KB
testcase_07 AC 272 ms
125,008 KB
testcase_08 AC 281 ms
124,672 KB
testcase_09 AC 265 ms
125,112 KB
testcase_10 AC 256 ms
125,052 KB
testcase_11 AC 269 ms
124,928 KB
testcase_12 AC 266 ms
124,544 KB
testcase_13 AC 261 ms
125,056 KB
testcase_14 AC 274 ms
124,928 KB
testcase_15 AC 289 ms
125,056 KB
testcase_16 AC 265 ms
125,056 KB
testcase_17 AC 278 ms
125,056 KB
testcase_18 AC 271 ms
124,800 KB
testcase_19 AC 483 ms
126,720 KB
testcase_20 AC 421 ms
142,592 KB
testcase_21 AC 325 ms
112,000 KB
testcase_22 AC 404 ms
117,932 KB
testcase_23 AC 411 ms
117,760 KB
testcase_24 AC 403 ms
133,760 KB
testcase_25 AC 396 ms
115,200 KB
testcase_26 AC 403 ms
136,960 KB
testcase_27 AC 440 ms
134,272 KB
testcase_28 AC 425 ms
123,648 KB
testcase_29 AC 445 ms
131,200 KB
testcase_30 AC 364 ms
113,024 KB
testcase_31 AC 457 ms
129,152 KB
testcase_32 AC 517 ms
115,456 KB
testcase_33 AC 492 ms
122,624 KB
testcase_34 AC 419 ms
142,412 KB
testcase_35 AC 493 ms
136,832 KB
testcase_36 AC 385 ms
116,076 KB
testcase_37 AC 466 ms
121,304 KB
testcase_38 AC 388 ms
115,968 KB
testcase_39 AC 417 ms
118,816 KB
testcase_40 AC 464 ms
133,888 KB
testcase_41 AC 428 ms
115,200 KB
testcase_42 AC 470 ms
126,720 KB
testcase_43 AC 499 ms
124,928 KB
testcase_44 AC 266 ms
124,928 KB
testcase_45 AC 285 ms
124,996 KB
testcase_46 AC 314 ms
124,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

###スニペット始まり###
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
0