結果

問題 No.2290 UnUnion Find
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-05 22:30:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 938 ms / 2,000 ms
コード長 1,900 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 109,696 KB
最終ジャッジ日時 2024-05-02 17:18:56
合計ジャッジ時間 34,665 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,888 KB
testcase_01 AC 40 ms
53,760 KB
testcase_02 AC 476 ms
77,184 KB
testcase_03 AC 399 ms
92,160 KB
testcase_04 AC 405 ms
91,776 KB
testcase_05 AC 415 ms
92,160 KB
testcase_06 AC 393 ms
92,032 KB
testcase_07 AC 433 ms
92,032 KB
testcase_08 AC 428 ms
91,904 KB
testcase_09 AC 445 ms
91,904 KB
testcase_10 AC 405 ms
91,776 KB
testcase_11 AC 431 ms
91,776 KB
testcase_12 AC 409 ms
92,032 KB
testcase_13 AC 409 ms
92,160 KB
testcase_14 AC 429 ms
91,904 KB
testcase_15 AC 432 ms
91,904 KB
testcase_16 AC 449 ms
91,776 KB
testcase_17 AC 477 ms
91,904 KB
testcase_18 AC 458 ms
91,904 KB
testcase_19 AC 938 ms
94,208 KB
testcase_20 AC 797 ms
109,696 KB
testcase_21 AC 668 ms
79,876 KB
testcase_22 AC 809 ms
84,640 KB
testcase_23 AC 820 ms
84,632 KB
testcase_24 AC 866 ms
100,864 KB
testcase_25 AC 779 ms
82,500 KB
testcase_26 AC 899 ms
104,064 KB
testcase_27 AC 852 ms
100,864 KB
testcase_28 AC 820 ms
90,496 KB
testcase_29 AC 860 ms
98,432 KB
testcase_30 AC 721 ms
80,824 KB
testcase_31 AC 905 ms
95,872 KB
testcase_32 AC 790 ms
82,536 KB
testcase_33 AC 855 ms
88,576 KB
testcase_34 AC 795 ms
109,696 KB
testcase_35 AC 829 ms
103,680 KB
testcase_36 AC 766 ms
82,380 KB
testcase_37 AC 799 ms
87,168 KB
testcase_38 AC 810 ms
82,864 KB
testcase_39 AC 817 ms
85,120 KB
testcase_40 AC 869 ms
100,992 KB
testcase_41 AC 722 ms
81,740 KB
testcase_42 AC 848 ms
93,952 KB
testcase_43 AC 867 ms
92,160 KB
testcase_44 AC 439 ms
92,032 KB
testcase_45 AC 429 ms
91,904 KB
testcase_46 AC 447 ms
92,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)]
    
from collections import deque

n,q= map(int,input().split())
nots = set()
uf = UnionFind(n+1)
for i in range(1,n+1):
    nots.add(i)
s=set()
lis=deque()
for i in range(q):
    t = list(map(int,input().split()))
    if t[0] == 1:
        x,y=t[1],t[2]
        if x > y:
            x,y = y,x
        nots.discard(x)
        nots.discard(y)
        uf.union(x,y)
        fx=uf.find(x)
        if fx not in s:
            s.add(x)
            lis.append(x)
    else:
        x=t[1]
        fx=uf.find(x)
        if fx not in s:
            xx=x+1
            if xx == n+1:
                xx = 1
            print(xx)
            
        else:
            if uf.size(fx) == n:
                print(-1)
            elif len(nots) > 0:
                for i in nots:
                    print(i)
                    break
            else:
                while len(lis)>1:
                    if fx == lis[-1]:
                        lis.appendleft(lis.pop())
                    else:
                        if fx == uf.find(lis[-1]):
                            lis.pop()
                        else:
                            print(lis[-1])
                            break
                            
                            
                    
            
            


0