結果

問題 No.2290 UnUnion Find
ユーザー buey_tbuey_t
提出日時 2023-05-08 11:51:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,162 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 123,844 KB
最終ジャッジ日時 2024-11-25 05:30:22
合計ジャッジ時間 23,305 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
89,472 KB
testcase_01 AC 136 ms
89,088 KB
testcase_02 AC 209 ms
90,368 KB
testcase_03 AC 258 ms
105,160 KB
testcase_04 AC 257 ms
105,088 KB
testcase_05 AC 260 ms
105,344 KB
testcase_06 AC 256 ms
105,088 KB
testcase_07 AC 259 ms
105,260 KB
testcase_08 AC 264 ms
105,224 KB
testcase_09 AC 258 ms
105,028 KB
testcase_10 AC 265 ms
105,472 KB
testcase_11 AC 263 ms
105,272 KB
testcase_12 AC 266 ms
105,340 KB
testcase_13 AC 268 ms
105,088 KB
testcase_14 AC 268 ms
105,216 KB
testcase_15 AC 270 ms
105,432 KB
testcase_16 AC 274 ms
105,404 KB
testcase_17 AC 262 ms
105,472 KB
testcase_18 AC 264 ms
105,088 KB
testcase_19 WA -
testcase_20 AC 451 ms
123,844 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 497 ms
118,188 KB
testcase_27 AC 605 ms
115,016 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 601 ms
109,824 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 508 ms
123,776 KB
testcase_35 AC 515 ms
118,016 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 505 ms
115,072 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 560 ms
105,472 KB
testcase_44 AC 274 ms
105,468 KB
testcase_45 AC 280 ms
105,472 KB
testcase_46 AC 302 ms
105,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum
from heapq import heapify, heappop, heappush
from bisect import bisect_left, bisect_right
from copy import deepcopy
import copy
import random
from random import randrange
from collections import deque,Counter,defaultdict
from itertools import permutations,combinations
from decimal import Decimal,ROUND_HALF_UP
#tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
from functools import lru_cache, reduce
#@lru_cache(maxsize=None)
from operator import add,sub,mul,xor,and_,or_,itemgetter
import sys
input = sys.stdin.readline
# .rstrip()
INF = 10**18
mod1 = 10**9+7
mod2 = 998244353

#DecimalならPython
#再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!!

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        #親だけ
        self.edge = [0]*n
        self.count = 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 self.parents[x] > self.parents[y]:
            x, y = y, x
        
        self.edge[x] += 1
        
        if x == y:
            return
        
        self.count -= 1

        self.parents[x] += self.parents[y]
        self.edge[x] += self.edge[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 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())

uf = UnionFind(N+1)

l = 1
r = N

f = 0
s = 0

st = set()
for i in range(1,N+1):
    st.add(i)

for i in range(Q):
    tmp = list(map(int, input().split()))
    
    if tmp[0] == 1:
        t,u,v = tmp
        
        uf.union(u,v)
        st.discard(u)
        st.discard(v)
        
        if f == 0:
            f = u
        else:
            if not uf.same(f,u) and s == 0:
                s = u
    
    else:
        t,v = tmp
        
        if uf.count == 2:
            print(-1)
        elif uf.same(f,v):
            if s == 0:
                for z in st:
                    if z != v:
                        print(z)
                        break
            else:
                print(s)
        elif uf.same(s,v):
            print(f)
        else:
            if f == 0:
                for z in st:
                    if z != v:
                        print(z)
                        break
            else:
                print(f)

























0