結果

問題 No.2290 UnUnion Find
ユーザー lloyzlloyz
提出日時 2023-05-05 21:31:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 844 ms / 2,000 ms
コード長 2,526 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 110,976 KB
最終ジャッジ日時 2024-05-02 15:23:57
合計ジャッジ時間 32,003 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,888 KB
testcase_01 AC 41 ms
53,760 KB
testcase_02 AC 449 ms
77,568 KB
testcase_03 AC 375 ms
92,288 KB
testcase_04 AC 379 ms
92,544 KB
testcase_05 AC 397 ms
92,288 KB
testcase_06 AC 390 ms
92,288 KB
testcase_07 AC 440 ms
92,160 KB
testcase_08 AC 438 ms
92,288 KB
testcase_09 AC 439 ms
92,288 KB
testcase_10 AC 403 ms
92,032 KB
testcase_11 AC 419 ms
92,160 KB
testcase_12 AC 415 ms
92,288 KB
testcase_13 AC 394 ms
92,416 KB
testcase_14 AC 401 ms
92,544 KB
testcase_15 AC 411 ms
92,288 KB
testcase_16 AC 426 ms
92,288 KB
testcase_17 AC 390 ms
92,160 KB
testcase_18 AC 426 ms
92,032 KB
testcase_19 AC 817 ms
94,720 KB
testcase_20 AC 758 ms
110,976 KB
testcase_21 AC 658 ms
80,128 KB
testcase_22 AC 759 ms
84,480 KB
testcase_23 AC 768 ms
84,608 KB
testcase_24 AC 654 ms
101,888 KB
testcase_25 AC 714 ms
82,808 KB
testcase_26 AC 630 ms
105,088 KB
testcase_27 AC 769 ms
102,272 KB
testcase_28 AC 739 ms
90,752 KB
testcase_29 AC 802 ms
99,328 KB
testcase_30 AC 712 ms
81,152 KB
testcase_31 AC 758 ms
96,640 KB
testcase_32 AC 734 ms
81,920 KB
testcase_33 AC 833 ms
89,216 KB
testcase_34 AC 712 ms
110,848 KB
testcase_35 AC 811 ms
105,472 KB
testcase_36 AC 749 ms
82,816 KB
testcase_37 AC 826 ms
87,936 KB
testcase_38 AC 768 ms
82,944 KB
testcase_39 AC 745 ms
85,504 KB
testcase_40 AC 807 ms
102,400 KB
testcase_41 AC 810 ms
82,896 KB
testcase_42 AC 828 ms
94,848 KB
testcase_43 AC 844 ms
92,544 KB
testcase_44 AC 400 ms
92,032 KB
testcase_45 AC 436 ms
92,416 KB
testcase_46 AC 469 ms
92,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n, q = map(int, input().split())
UF = UnionFind(n)
root_set = set(list(range(n)))
for _ in range(q):
    L = list(map(int, input().split()))
    if L[0] == 1:
        u, v = L[1:]
        u -= 1
        v -= 1
        if UF.same(u, v):
            continue
        uroot = UF.find(u)
        vroot = UF.find(v)
        UF.union(u, v)
        root = UF.find(u)
        if root == uroot:
            root_set.remove(vroot)
        else:
            root_set.remove(uroot)
    else:
        v = L[1]
        v -= 1
        if len(root_set) == 1:
            print(-1)
        else:
            root = UF.find(v)
            for i in root_set:
                if i != root:
                    print(i + 1)
                    break
0