結果

問題 No.2290 UnUnion Find
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-05-05 21:47:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 502 ms
コンパイル使用メモリ 86,940 KB
実行使用メモリ 112,716 KB
最終ジャッジ日時 2023-08-15 03:35:47
合計ジャッジ時間 36,633 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,320 KB
testcase_01 AC 72 ms
71,576 KB
testcase_02 AC 527 ms
78,808 KB
testcase_03 AC 460 ms
93,840 KB
testcase_04 AC 459 ms
93,824 KB
testcase_05 AC 468 ms
93,676 KB
testcase_06 AC 455 ms
93,940 KB
testcase_07 AC 477 ms
93,768 KB
testcase_08 AC 484 ms
93,912 KB
testcase_09 AC 482 ms
93,776 KB
testcase_10 AC 460 ms
93,772 KB
testcase_11 AC 476 ms
93,820 KB
testcase_12 AC 469 ms
93,772 KB
testcase_13 AC 464 ms
93,620 KB
testcase_14 AC 475 ms
93,672 KB
testcase_15 AC 480 ms
93,672 KB
testcase_16 AC 462 ms
93,816 KB
testcase_17 AC 466 ms
93,992 KB
testcase_18 AC 476 ms
93,888 KB
testcase_19 AC 888 ms
95,936 KB
testcase_20 AC 778 ms
112,716 KB
testcase_21 AC 716 ms
82,256 KB
testcase_22 AC 807 ms
86,016 KB
testcase_23 AC 851 ms
86,336 KB
testcase_24 AC 713 ms
103,064 KB
testcase_25 AC 762 ms
83,580 KB
testcase_26 AC 709 ms
106,804 KB
testcase_27 AC 804 ms
103,536 KB
testcase_28 AC 816 ms
92,124 KB
testcase_29 AC 852 ms
101,140 KB
testcase_30 AC 768 ms
82,568 KB
testcase_31 AC 808 ms
98,072 KB
testcase_32 AC 793 ms
84,944 KB
testcase_33 AC 871 ms
90,668 KB
testcase_34 AC 757 ms
112,560 KB
testcase_35 AC 834 ms
106,740 KB
testcase_36 AC 804 ms
84,012 KB
testcase_37 AC 879 ms
88,872 KB
testcase_38 AC 802 ms
83,816 KB
testcase_39 AC 821 ms
86,788 KB
testcase_40 AC 882 ms
103,356 KB
testcase_41 AC 852 ms
83,748 KB
testcase_42 AC 878 ms
96,156 KB
testcase_43 AC 904 ms
94,416 KB
testcase_44 AC 475 ms
93,644 KB
testcase_45 AC 489 ms
93,708 KB
testcase_46 AC 515 ms
94,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
        self.ps = set([i for i in range(n)])
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        bx,by = x,y
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]

        self.uf[y] = x
        if bx != x:
            self.ps.remove(bx)
        if by != x:
            self.ps.remove(by)
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]



n,q = map(int,input().split())
uf = Unionfind(n)
for i in range(q):
    l = list(map(int,input().split()))
    if l[0] == 1:
        u,v = l[1:]
        uf.union(u-1,v-1)
    else:
        u = l[1]
        p = uf.find(u-1)
        ans = -1
        for x in uf.ps:
            if x != p:
                ans = x+1
                break
        print(ans)
0