結果

問題 No.2290 UnUnion Find
ユーザー yabityabit
提出日時 2023-06-30 14:19:12
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,957 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 111,012 KB
最終ジャッジ日時 2023-09-21 07:45:03
合計ジャッジ時間 28,852 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
75,748 KB
testcase_01 AC 94 ms
71,512 KB
testcase_02 AC 572 ms
77,968 KB
testcase_03 AC 486 ms
92,632 KB
testcase_04 AC 487 ms
92,888 KB
testcase_05 AC 496 ms
92,884 KB
testcase_06 AC 480 ms
92,788 KB
testcase_07 AC 501 ms
92,908 KB
testcase_08 AC 501 ms
92,984 KB
testcase_09 AC 509 ms
92,860 KB
testcase_10 AC 481 ms
92,908 KB
testcase_11 AC 509 ms
92,856 KB
testcase_12 AC 486 ms
92,928 KB
testcase_13 AC 487 ms
92,868 KB
testcase_14 AC 496 ms
93,012 KB
testcase_15 AC 505 ms
92,668 KB
testcase_16 AC 483 ms
92,928 KB
testcase_17 AC 486 ms
92,932 KB
testcase_18 AC 487 ms
92,648 KB
testcase_19 AC 1,054 ms
95,676 KB
testcase_20 AC 906 ms
111,012 KB
testcase_21 TLE -
testcase_22 AC 1,003 ms
86,824 KB
testcase_23 AC 986 ms
86,720 KB
testcase_24 AC 812 ms
102,492 KB
testcase_25 AC 1,055 ms
87,928 KB
testcase_26 AC 798 ms
105,256 KB
testcase_27 AC 914 ms
102,052 KB
testcase_28 AC 931 ms
91,796 KB
testcase_29 AC 988 ms
99,300 KB
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
 # UnionFind(n) n:頂点数
 # parents:i番目の点が属する根 それ自身が根なら-1
 # find(x):xの属する根を再帰的に求める
 # unite(x,y):多い木に小さい木を結合
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, 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 = 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):
        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)
roots = set(range(N))
for q in range(Q):
    L = list(map(int, input().split()))
    if L[0] == 1:
        uf.union(L[1]-1, L[2]-1)
        if L[1]-1 != uf.find(L[1]-1):
            roots.discard(L[1]-1)
        if L[2]-1 != uf.find(L[2]-1):
            roots.discard(L[2]-1)
    else:
        ret = -1
        for r in roots:
            if not uf.same(r, L[1]-1):
                ret = r+1
                break
        print(ret)
0