結果

問題 No.2290 UnUnion Find
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-05 22:30:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,035 ms / 2,000 ms
コード長 1,900 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 86,412 KB
実行使用メモリ 111,068 KB
最終ジャッジ日時 2023-08-15 05:10:17
合計ジャッジ時間 41,958 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
71,168 KB
testcase_01 AC 101 ms
71,136 KB
testcase_02 AC 594 ms
77,764 KB
testcase_03 AC 503 ms
92,856 KB
testcase_04 AC 504 ms
92,988 KB
testcase_05 AC 526 ms
92,808 KB
testcase_06 AC 505 ms
92,784 KB
testcase_07 AC 539 ms
92,912 KB
testcase_08 AC 549 ms
93,132 KB
testcase_09 AC 546 ms
92,908 KB
testcase_10 AC 511 ms
92,908 KB
testcase_11 AC 535 ms
92,880 KB
testcase_12 AC 509 ms
93,148 KB
testcase_13 AC 509 ms
92,944 KB
testcase_14 AC 541 ms
92,884 KB
testcase_15 AC 532 ms
92,924 KB
testcase_16 AC 535 ms
93,128 KB
testcase_17 AC 507 ms
92,924 KB
testcase_18 AC 544 ms
93,128 KB
testcase_19 AC 1,035 ms
95,384 KB
testcase_20 AC 927 ms
110,996 KB
testcase_21 AC 762 ms
81,064 KB
testcase_22 AC 954 ms
87,956 KB
testcase_23 AC 938 ms
87,076 KB
testcase_24 AC 1,019 ms
102,484 KB
testcase_25 AC 893 ms
85,052 KB
testcase_26 AC 1,004 ms
105,772 KB
testcase_27 AC 1,014 ms
102,144 KB
testcase_28 AC 956 ms
91,360 KB
testcase_29 AC 999 ms
100,036 KB
testcase_30 AC 804 ms
83,120 KB
testcase_31 AC 1,023 ms
97,272 KB
testcase_32 AC 887 ms
84,292 KB
testcase_33 AC 1,003 ms
90,324 KB
testcase_34 AC 933 ms
111,068 KB
testcase_35 AC 986 ms
105,656 KB
testcase_36 AC 900 ms
84,436 KB
testcase_37 AC 970 ms
89,060 KB
testcase_38 AC 896 ms
85,368 KB
testcase_39 AC 923 ms
87,028 KB
testcase_40 AC 1,007 ms
102,680 KB
testcase_41 AC 864 ms
84,612 KB
testcase_42 AC 1,031 ms
95,340 KB
testcase_43 AC 1,001 ms
93,664 KB
testcase_44 AC 513 ms
92,900 KB
testcase_45 AC 513 ms
93,176 KB
testcase_46 AC 536 ms
93,076 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