結果

問題 No.2290 UnUnion Find
ユーザー AEnAEn
提出日時 2023-07-26 00:17:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,123 ms / 2,000 ms
コード長 2,780 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 145,072 KB
最終ジャッジ日時 2024-04-10 15:14:26
合計ジャッジ時間 42,385 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
67,932 KB
testcase_01 AC 58 ms
68,048 KB
testcase_02 AC 491 ms
78,752 KB
testcase_03 AC 478 ms
108,932 KB
testcase_04 AC 499 ms
109,236 KB
testcase_05 AC 500 ms
108,956 KB
testcase_06 AC 538 ms
108,844 KB
testcase_07 AC 519 ms
108,960 KB
testcase_08 AC 521 ms
108,896 KB
testcase_09 AC 531 ms
108,860 KB
testcase_10 AC 543 ms
109,324 KB
testcase_11 AC 530 ms
108,776 KB
testcase_12 AC 500 ms
109,248 KB
testcase_13 AC 493 ms
109,116 KB
testcase_14 AC 521 ms
109,256 KB
testcase_15 AC 514 ms
108,872 KB
testcase_16 AC 488 ms
109,036 KB
testcase_17 AC 516 ms
109,168 KB
testcase_18 AC 527 ms
108,920 KB
testcase_19 AC 1,117 ms
112,928 KB
testcase_20 AC 912 ms
145,072 KB
testcase_21 AC 762 ms
82,088 KB
testcase_22 AC 957 ms
93,264 KB
testcase_23 AC 1,042 ms
93,752 KB
testcase_24 AC 916 ms
127,436 KB
testcase_25 AC 883 ms
88,292 KB
testcase_26 AC 857 ms
133,980 KB
testcase_27 AC 1,007 ms
127,828 KB
testcase_28 AC 943 ms
105,460 KB
testcase_29 AC 1,096 ms
121,824 KB
testcase_30 AC 886 ms
83,816 KB
testcase_31 AC 1,019 ms
117,380 KB
testcase_32 AC 892 ms
88,348 KB
testcase_33 AC 1,069 ms
102,320 KB
testcase_34 AC 878 ms
144,876 KB
testcase_35 AC 1,037 ms
133,792 KB
testcase_36 AC 942 ms
89,652 KB
testcase_37 AC 1,123 ms
99,748 KB
testcase_38 AC 1,025 ms
89,636 KB
testcase_39 AC 977 ms
95,124 KB
testcase_40 AC 1,025 ms
127,748 KB
testcase_41 AC 1,103 ms
87,128 KB
testcase_42 AC 1,074 ms
112,908 KB
testcase_43 AC 1,035 ms
108,880 KB
testcase_44 AC 461 ms
108,964 KB
testcase_45 AC 499 ms
108,848 KB
testcase_46 AC 550 ms
109,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_coun

N,Q = map(int, input().split())
d = {}
s = set()
uf = UnionFind(N)
for i in range(N):
    d[uf.root(i)] = i
    s.add(i)

for i in range(Q):
    q = list(map(int, input().split()))
    if q[0]==1:
        a,b = q[1]-1,q[2]-1
        ra,rb = uf.root(a),uf.root(b)
        if ra==rb:continue
        uf.unite(a,b)
        rab = uf.root(a)
        if ra==rab:
            s.remove(d[rb])
        else:
            s.remove(d[ra])
    else:
        a = q[1]-1
        if len(s)==1:
            print(-1)
        else:
            r = uf.root(a)
            for s_ in s:
                if s_!=d[r]:
                    print(s_+1)
                    break

0