結果

問題 No.2290 UnUnion Find
ユーザー noriocnorioc
提出日時 2023-05-10 04:03:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 916 ms / 2,000 ms
コード長 1,703 bytes
コンパイル時間 1,041 ms
コンパイル使用メモリ 85,884 KB
実行使用メモリ 113,692 KB
最終ジャッジ日時 2023-08-17 13:59:23
合計ジャッジ時間 37,174 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,260 KB
testcase_01 AC 68 ms
71,404 KB
testcase_02 AC 561 ms
78,600 KB
testcase_03 AC 452 ms
93,836 KB
testcase_04 AC 454 ms
94,180 KB
testcase_05 AC 477 ms
93,952 KB
testcase_06 AC 452 ms
94,212 KB
testcase_07 AC 475 ms
93,936 KB
testcase_08 AC 481 ms
94,160 KB
testcase_09 AC 476 ms
93,940 KB
testcase_10 AC 461 ms
94,124 KB
testcase_11 AC 476 ms
94,200 KB
testcase_12 AC 462 ms
94,268 KB
testcase_13 AC 463 ms
94,124 KB
testcase_14 AC 483 ms
94,044 KB
testcase_15 AC 487 ms
94,048 KB
testcase_16 AC 464 ms
94,216 KB
testcase_17 AC 468 ms
94,288 KB
testcase_18 AC 487 ms
94,252 KB
testcase_19 AC 916 ms
96,644 KB
testcase_20 AC 798 ms
113,532 KB
testcase_21 AC 741 ms
81,768 KB
testcase_22 AC 844 ms
85,856 KB
testcase_23 AC 849 ms
87,376 KB
testcase_24 AC 736 ms
104,060 KB
testcase_25 AC 800 ms
85,188 KB
testcase_26 AC 709 ms
108,012 KB
testcase_27 AC 844 ms
104,444 KB
testcase_28 AC 838 ms
92,516 KB
testcase_29 AC 861 ms
101,244 KB
testcase_30 AC 807 ms
82,888 KB
testcase_31 AC 824 ms
98,624 KB
testcase_32 AC 803 ms
83,920 KB
testcase_33 AC 859 ms
90,836 KB
testcase_34 AC 769 ms
113,692 KB
testcase_35 AC 887 ms
107,408 KB
testcase_36 AC 871 ms
85,428 KB
testcase_37 AC 892 ms
89,664 KB
testcase_38 AC 822 ms
85,124 KB
testcase_39 AC 820 ms
87,004 KB
testcase_40 AC 883 ms
104,768 KB
testcase_41 AC 869 ms
84,540 KB
testcase_42 AC 884 ms
96,452 KB
testcase_43 AC 904 ms
94,864 KB
testcase_44 AC 469 ms
94,164 KB
testcase_45 AC 500 ms
94,144 KB
testcase_46 AC 506 ms
94,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.data = [-1] * (n + 1)
        self.nexts = [-1] * (n + 1) # 次の要素(なければ-1)
        self.tails = list(range(n + 1)) # 末尾の要素

    def root(self, a: int) -> int:
        if self.data[a] < 0: return a
        self.data[a] = self.root(self.data[a])
        return self.data[a]

    def unite(self, a: int, b: int) -> bool:
        pa = self.root(a)
        pb = self.root(b)
        if pa == pb: return False
        if self.data[pa] > self.data[pb]:
            pa, pb = pb, pa
        self.data[pa] += self.data[pb] # pa を pb をつなげる
        self.data[pb] = pa
        # pa の末尾に pb を繋げる
        self.nexts[self.tails[pa]] = pb
        self.tails[pa] = self.tails[pb]
        return True

    def issame(self, a: int, b: int) -> bool:
        return self.root(a) == self.root(b)

    def size(self, a: int) -> int:
        """a が属する集合のサイズ"""
        return -self.data[self.root(a)]

    def group(self, a):
        """a が属する集合"""
        v = self.root(a)
        while v != -1:
            yield v
            v = self.nexts[v]


N, Q = map(int, input().split())
leaders = set(range(1, N+1))
uf = UnionFind(N+10)
for _ in range(Q):
    t, *a = map(int, input().split())
    if t == 1:
        u, v = a
        u = uf.root(u)
        v = uf.root(v)
        uf.unite(u, v)
        r = uf.root(u)
        if r != u: leaders.discard(u)
        if r != v: leaders.discard(v)
    elif t == 2:
        v = a[0]
        r = uf.root(v)
        for x in leaders:
            if r != uf.root(x):
                print(x)
                break
        else:
            print(-1)
0