結果

問題 No.2290 UnUnion Find
ユーザー buey_tbuey_t
提出日時 2023-05-08 13:09:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 710 ms / 2,000 ms
コード長 2,671 bytes
コンパイル時間 1,378 ms
コンパイル使用メモリ 86,232 KB
実行使用メモリ 96,068 KB
最終ジャッジ日時 2023-08-16 16:45:16
合計ジャッジ時間 28,950 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 203 ms
82,364 KB
testcase_01 AC 204 ms
82,200 KB
testcase_02 AC 319 ms
84,084 KB
testcase_03 AC 317 ms
89,520 KB
testcase_04 AC 326 ms
89,680 KB
testcase_05 AC 341 ms
89,636 KB
testcase_06 AC 328 ms
91,784 KB
testcase_07 AC 333 ms
89,644 KB
testcase_08 AC 324 ms
89,580 KB
testcase_09 AC 327 ms
92,024 KB
testcase_10 AC 332 ms
92,008 KB
testcase_11 AC 323 ms
89,648 KB
testcase_12 AC 333 ms
89,452 KB
testcase_13 AC 325 ms
92,356 KB
testcase_14 AC 333 ms
89,588 KB
testcase_15 AC 330 ms
89,728 KB
testcase_16 AC 333 ms
89,760 KB
testcase_17 AC 324 ms
89,712 KB
testcase_18 AC 330 ms
89,708 KB
testcase_19 AC 602 ms
92,528 KB
testcase_20 AC 524 ms
90,596 KB
testcase_21 AC 425 ms
86,480 KB
testcase_22 AC 548 ms
93,044 KB
testcase_23 AC 528 ms
90,160 KB
testcase_24 AC 578 ms
96,068 KB
testcase_25 AC 542 ms
86,548 KB
testcase_26 AC 422 ms
88,344 KB
testcase_27 AC 467 ms
88,344 KB
testcase_28 AC 563 ms
91,224 KB
testcase_29 AC 538 ms
94,020 KB
testcase_30 AC 516 ms
88,168 KB
testcase_31 AC 564 ms
94,368 KB
testcase_32 AC 555 ms
88,744 KB
testcase_33 AC 502 ms
89,920 KB
testcase_34 AC 440 ms
88,996 KB
testcase_35 AC 523 ms
89,652 KB
testcase_36 AC 499 ms
90,044 KB
testcase_37 AC 575 ms
92,004 KB
testcase_38 AC 491 ms
88,292 KB
testcase_39 AC 503 ms
88,904 KB
testcase_40 AC 504 ms
88,608 KB
testcase_41 AC 502 ms
88,780 KB
testcase_42 AC 665 ms
93,792 KB
testcase_43 AC 710 ms
93,224 KB
testcase_44 AC 344 ms
89,620 KB
testcase_45 AC 335 ms
87,820 KB
testcase_46 AC 358 ms
90,660 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

for i in range(Q):
    tmp = list(map(int, input().split()))
    
    if tmp[0] == 1:
        t,u,v = tmp
        
        uf.union(u,v)
        while uf.find(l) != l:
            l += 1
        while uf.find(r) != r:
            r -= 1
        
    else:
        t,v = tmp
        
        if l == r:
            print(-1)
        else:
            if uf.same(r,v):
                print(l)
            else:
                print(r)

























0