結果

問題 No.2290 UnUnion Find
ユーザー chineristACchineristAC
提出日時 2023-05-05 21:25:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 653 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 90,300 KB
最終ジャッジ日時 2023-08-15 02:48:28
合計ジャッジ時間 25,118 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
74,348 KB
testcase_01 AC 111 ms
74,736 KB
testcase_02 AC 249 ms
83,128 KB
testcase_03 AC 294 ms
83,468 KB
testcase_04 AC 292 ms
83,992 KB
testcase_05 AC 297 ms
83,748 KB
testcase_06 AC 302 ms
84,256 KB
testcase_07 AC 293 ms
84,068 KB
testcase_08 AC 294 ms
83,684 KB
testcase_09 AC 291 ms
84,060 KB
testcase_10 AC 294 ms
83,860 KB
testcase_11 AC 297 ms
84,328 KB
testcase_12 AC 299 ms
83,952 KB
testcase_13 AC 303 ms
83,544 KB
testcase_14 AC 297 ms
83,920 KB
testcase_15 AC 305 ms
84,012 KB
testcase_16 AC 294 ms
83,976 KB
testcase_17 AC 293 ms
83,952 KB
testcase_18 AC 294 ms
84,252 KB
testcase_19 AC 570 ms
88,628 KB
testcase_20 AC 457 ms
89,284 KB
testcase_21 AC 401 ms
85,896 KB
testcase_22 AC 552 ms
89,580 KB
testcase_23 AC 454 ms
86,636 KB
testcase_24 AC 428 ms
87,136 KB
testcase_25 AC 528 ms
87,976 KB
testcase_26 AC 432 ms
86,748 KB
testcase_27 AC 477 ms
87,916 KB
testcase_28 AC 518 ms
88,352 KB
testcase_29 AC 533 ms
88,760 KB
testcase_30 AC 474 ms
87,380 KB
testcase_31 AC 506 ms
87,556 KB
testcase_32 AC 549 ms
88,820 KB
testcase_33 AC 460 ms
86,256 KB
testcase_34 AC 425 ms
87,632 KB
testcase_35 AC 537 ms
89,360 KB
testcase_36 AC 428 ms
85,720 KB
testcase_37 AC 536 ms
88,632 KB
testcase_38 AC 496 ms
87,276 KB
testcase_39 AC 459 ms
87,708 KB
testcase_40 AC 506 ms
88,412 KB
testcase_41 AC 513 ms
86,988 KB
testcase_42 AC 653 ms
90,300 KB
testcase_43 AC 597 ms
88,812 KB
testcase_44 AC 291 ms
84,424 KB
testcase_45 AC 282 ms
85,840 KB
testcase_46 AC 296 ms
85,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)


N,Q = mi()
a = 1
uf = UnionFindVerSize(N)
for _ in range(Q):
    t,*query = mi()
    if t == 1:
        u,v = query
        u,v = u-1,v-1
        uf.unite(u,v)
        while a!=N and uf.is_same_group(0,a):
            a += 1
    else:
        v = query[0]
        v -= 1
        if not uf.is_same_group(0,v):
            print(1)
        elif a!=N:
            print(a+1)
        else:
            print(-1)
0