結果

問題 No.2290 UnUnion Find
ユーザー lloyzlloyz
提出日時 2023-05-05 21:31:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,042 ms / 2,000 ms
コード長 2,526 bytes
コンパイル時間 1,267 ms
コンパイル使用メモリ 86,728 KB
実行使用メモリ 113,104 KB
最終ジャッジ日時 2023-08-15 03:07:38
合計ジャッジ時間 39,699 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,748 KB
testcase_01 AC 93 ms
71,780 KB
testcase_02 AC 577 ms
78,096 KB
testcase_03 AC 460 ms
93,872 KB
testcase_04 AC 479 ms
93,788 KB
testcase_05 AC 488 ms
93,936 KB
testcase_06 AC 480 ms
93,796 KB
testcase_07 AC 506 ms
93,660 KB
testcase_08 AC 498 ms
93,680 KB
testcase_09 AC 495 ms
93,804 KB
testcase_10 AC 493 ms
94,008 KB
testcase_11 AC 491 ms
93,856 KB
testcase_12 AC 501 ms
93,892 KB
testcase_13 AC 484 ms
93,840 KB
testcase_14 AC 498 ms
93,920 KB
testcase_15 AC 499 ms
93,884 KB
testcase_16 AC 480 ms
93,828 KB
testcase_17 AC 486 ms
93,912 KB
testcase_18 AC 503 ms
93,928 KB
testcase_19 AC 998 ms
96,652 KB
testcase_20 AC 865 ms
113,104 KB
testcase_21 AC 774 ms
82,488 KB
testcase_22 AC 907 ms
87,104 KB
testcase_23 AC 935 ms
87,796 KB
testcase_24 AC 805 ms
102,960 KB
testcase_25 AC 873 ms
85,520 KB
testcase_26 AC 775 ms
107,016 KB
testcase_27 AC 959 ms
103,456 KB
testcase_28 AC 911 ms
92,420 KB
testcase_29 AC 980 ms
100,496 KB
testcase_30 AC 828 ms
83,592 KB
testcase_31 AC 925 ms
98,384 KB
testcase_32 AC 921 ms
86,948 KB
testcase_33 AC 987 ms
90,716 KB
testcase_34 AC 836 ms
112,260 KB
testcase_35 AC 989 ms
107,128 KB
testcase_36 AC 912 ms
86,440 KB
testcase_37 AC 1,003 ms
89,124 KB
testcase_38 AC 910 ms
84,768 KB
testcase_39 AC 906 ms
88,764 KB
testcase_40 AC 974 ms
103,744 KB
testcase_41 AC 969 ms
87,344 KB
testcase_42 AC 1,014 ms
95,612 KB
testcase_43 AC 1,042 ms
94,144 KB
testcase_44 AC 484 ms
93,968 KB
testcase_45 AC 517 ms
94,200 KB
testcase_46 AC 559 ms
94,340 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