結果

問題 No.2290 UnUnion Find
ユーザー kuro_Bkuro_B
提出日時 2023-05-09 17:58:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,072 bytes
コンパイル時間 343 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 142,592 KB
最終ジャッジ日時 2024-05-04 15:28:21
合計ジャッジ時間 20,470 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
82,432 KB
testcase_01 WA -
testcase_02 AC 228 ms
110,336 KB
testcase_03 AC 262 ms
125,056 KB
testcase_04 AC 288 ms
125,184 KB
testcase_05 AC 293 ms
125,056 KB
testcase_06 AC 260 ms
124,672 KB
testcase_07 AC 272 ms
124,928 KB
testcase_08 AC 285 ms
124,544 KB
testcase_09 AC 289 ms
124,544 KB
testcase_10 AC 265 ms
125,184 KB
testcase_11 AC 278 ms
125,184 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 275 ms
125,184 KB
testcase_15 AC 281 ms
125,184 KB
testcase_16 AC 273 ms
124,928 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 374 ms
133,632 KB
testcase_25 WA -
testcase_26 AC 393 ms
136,832 KB
testcase_27 AC 447 ms
134,016 KB
testcase_28 WA -
testcase_29 AC 455 ms
131,328 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 423 ms
122,240 KB
testcase_34 AC 390 ms
142,336 KB
testcase_35 AC 435 ms
136,960 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 463 ms
134,400 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 293 ms
124,544 KB
testcase_45 AC 309 ms
124,800 KB
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

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(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
0