結果

問題 No.2290 UnUnion Find
ユーザー noriocnorioc
提出日時 2023-05-05 21:55:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 898 ms / 2,000 ms
コード長 1,743 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 86,872 KB
実行使用メモリ 113,736 KB
最終ジャッジ日時 2023-08-15 03:54:44
合計ジャッジ時間 38,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,504 KB
testcase_01 AC 73 ms
71,444 KB
testcase_02 AC 567 ms
78,552 KB
testcase_03 AC 491 ms
94,176 KB
testcase_04 AC 502 ms
94,252 KB
testcase_05 AC 516 ms
94,232 KB
testcase_06 AC 501 ms
94,604 KB
testcase_07 AC 527 ms
94,608 KB
testcase_08 AC 527 ms
94,180 KB
testcase_09 AC 529 ms
94,660 KB
testcase_10 AC 506 ms
94,468 KB
testcase_11 AC 536 ms
94,252 KB
testcase_12 AC 515 ms
94,636 KB
testcase_13 AC 507 ms
94,428 KB
testcase_14 AC 520 ms
94,420 KB
testcase_15 AC 547 ms
94,296 KB
testcase_16 AC 507 ms
94,424 KB
testcase_17 AC 508 ms
94,304 KB
testcase_18 AC 530 ms
94,612 KB
testcase_19 AC 846 ms
96,548 KB
testcase_20 AC 775 ms
113,656 KB
testcase_21 AC 733 ms
81,504 KB
testcase_22 AC 795 ms
86,232 KB
testcase_23 AC 806 ms
86,172 KB
testcase_24 AC 870 ms
104,104 KB
testcase_25 AC 829 ms
85,036 KB
testcase_26 AC 822 ms
107,640 KB
testcase_27 AC 856 ms
104,472 KB
testcase_28 AC 842 ms
92,548 KB
testcase_29 AC 862 ms
101,536 KB
testcase_30 AC 772 ms
83,508 KB
testcase_31 AC 847 ms
98,844 KB
testcase_32 AC 817 ms
84,932 KB
testcase_33 AC 829 ms
91,004 KB
testcase_34 AC 781 ms
113,736 KB
testcase_35 AC 838 ms
107,468 KB
testcase_36 AC 816 ms
84,196 KB
testcase_37 AC 831 ms
89,692 KB
testcase_38 AC 838 ms
84,720 KB
testcase_39 AC 829 ms
86,968 KB
testcase_40 AC 841 ms
104,560 KB
testcase_41 AC 812 ms
83,512 KB
testcase_42 AC 898 ms
96,480 KB
testcase_43 AC 830 ms
94,432 KB
testcase_44 AC 518 ms
94,368 KB
testcase_45 AC 490 ms
94,104 KB
testcase_46 AC 519 ms
94,184 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())
kinds = 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
        x = uf.root(u)
        y = uf.root(v)
        uf.unite(u, v)
        r = uf.root(u)
        if x != r: kinds.discard(x)
        if y != r: kinds.discard(y)
    elif t == 2:
        v = a[0]
        if uf.size(v) == N:
            print(-1)
        else:
            r = uf.root(v)
            for x in kinds:
                if r != uf.root(x):
                    print(x)
                    break
0