結果

問題 No.2290 UnUnion Find
ユーザー OhnoHirokiOhnoHiroki
提出日時 2024-02-27 07:00:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 664 ms / 2,000 ms
コード長 1,422 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 111,068 KB
最終ジャッジ日時 2024-02-27 07:01:10
合計ジャッジ時間 27,741 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
53,460 KB
testcase_01 AC 30 ms
53,460 KB
testcase_02 AC 416 ms
76,012 KB
testcase_03 AC 337 ms
92,416 KB
testcase_04 AC 371 ms
92,416 KB
testcase_05 AC 370 ms
92,416 KB
testcase_06 AC 420 ms
92,416 KB
testcase_07 AC 372 ms
92,416 KB
testcase_08 AC 366 ms
92,416 KB
testcase_09 AC 390 ms
92,416 KB
testcase_10 AC 371 ms
92,416 KB
testcase_11 AC 373 ms
92,416 KB
testcase_12 AC 397 ms
92,416 KB
testcase_13 AC 375 ms
92,416 KB
testcase_14 AC 374 ms
92,416 KB
testcase_15 AC 394 ms
92,416 KB
testcase_16 AC 365 ms
92,416 KB
testcase_17 AC 371 ms
92,416 KB
testcase_18 AC 417 ms
92,416 KB
testcase_19 AC 636 ms
93,972 KB
testcase_20 AC 566 ms
110,304 KB
testcase_21 AC 549 ms
79,672 KB
testcase_22 AC 645 ms
84,712 KB
testcase_23 AC 591 ms
84,584 KB
testcase_24 AC 592 ms
102,020 KB
testcase_25 AC 633 ms
82,036 KB
testcase_26 AC 578 ms
106,672 KB
testcase_27 AC 621 ms
102,212 KB
testcase_28 AC 590 ms
89,932 KB
testcase_29 AC 595 ms
98,516 KB
testcase_30 AC 574 ms
80,184 KB
testcase_31 AC 603 ms
96,040 KB
testcase_32 AC 617 ms
81,996 KB
testcase_33 AC 664 ms
89,368 KB
testcase_34 AC 579 ms
111,068 KB
testcase_35 AC 613 ms
104,552 KB
testcase_36 AC 595 ms
82,736 KB
testcase_37 AC 596 ms
87,900 KB
testcase_38 AC 629 ms
82,708 KB
testcase_39 AC 628 ms
84,728 KB
testcase_40 AC 617 ms
101,592 KB
testcase_41 AC 631 ms
80,496 KB
testcase_42 AC 614 ms
93,652 KB
testcase_43 AC 578 ms
91,996 KB
testcase_44 AC 436 ms
92,416 KB
testcase_45 AC 358 ms
92,296 KB
testcase_46 AC 376 ms
92,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
UnionFind
"""
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)]
    
    def same(self, x, y):
        return self.find(x) == self.find(y)


N,Q = map(int, input().split())
vertex = set([i for i in range(N)])
UF = UnionFind(N)
for q in range(Q):
    INPUT = list(map(int, input().split()))
    if INPUT[0] == 1:
        v1 = UF.find(INPUT[1]-1)
        v2 = UF.find(INPUT[2]-1)
        UF.union(INPUT[1]-1, INPUT[2]-1)
        if UF.find(INPUT[1]-1) == v1:
            vertex.discard(v2)
        else:
            vertex.discard(v1)
    else:
        if len(vertex) == 1:
            print(-1)
        else:
            v1,v2 = vertex.pop(), vertex.pop()
            if v1 == UF.find(INPUT[1]-1):
                print(v2+1)
            else:
                print(v1+1)
            vertex.add(v1)
            vertex.add(v2)
0