結果

問題 No.2290 UnUnion Find
ユーザー 👑 H20H20
提出日時 2023-05-07 01:02:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 2,001 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 110,720 KB
最終ジャッジ日時 2024-05-03 06:55:57
合計ジャッジ時間 21,588 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,760 KB
testcase_01 AC 43 ms
54,144 KB
testcase_02 AC 255 ms
97,536 KB
testcase_03 AC 286 ms
98,560 KB
testcase_04 AC 283 ms
104,604 KB
testcase_05 AC 295 ms
104,464 KB
testcase_06 AC 291 ms
104,448 KB
testcase_07 AC 291 ms
104,732 KB
testcase_08 AC 289 ms
104,576 KB
testcase_09 AC 285 ms
104,392 KB
testcase_10 AC 293 ms
104,644 KB
testcase_11 AC 291 ms
104,808 KB
testcase_12 AC 288 ms
104,320 KB
testcase_13 AC 291 ms
104,612 KB
testcase_14 AC 299 ms
104,388 KB
testcase_15 AC 293 ms
104,540 KB
testcase_16 AC 296 ms
104,692 KB
testcase_17 AC 290 ms
104,320 KB
testcase_18 AC 297 ms
104,448 KB
testcase_19 AC 426 ms
103,984 KB
testcase_20 AC 397 ms
106,240 KB
testcase_21 AC 343 ms
101,248 KB
testcase_22 AC 379 ms
107,768 KB
testcase_23 AC 389 ms
108,792 KB
testcase_24 AC 403 ms
104,260 KB
testcase_25 AC 379 ms
109,440 KB
testcase_26 AC 397 ms
105,472 KB
testcase_27 AC 439 ms
104,832 KB
testcase_28 AC 386 ms
106,384 KB
testcase_29 AC 433 ms
103,680 KB
testcase_30 AC 361 ms
103,384 KB
testcase_31 AC 430 ms
103,604 KB
testcase_32 AC 376 ms
109,360 KB
testcase_33 AC 416 ms
106,264 KB
testcase_34 AC 385 ms
106,360 KB
testcase_35 AC 427 ms
105,352 KB
testcase_36 AC 381 ms
109,408 KB
testcase_37 AC 408 ms
107,340 KB
testcase_38 AC 385 ms
109,380 KB
testcase_39 AC 393 ms
108,096 KB
testcase_40 AC 431 ms
105,088 KB
testcase_41 AC 372 ms
110,720 KB
testcase_42 AC 419 ms
104,096 KB
testcase_43 AC 417 ms
104,948 KB
testcase_44 AC 300 ms
104,504 KB
testcase_45 AC 295 ms
104,472 KB
testcase_46 AC 293 ms
104,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = 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
        self.root-=1

    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 self.root

    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())
Q = [list(map(int, input().split())) for _ in range(q)]
uf = UnionFind(N)
STACK = []
m1_cnt = 0
is_all_union = N==1
for q in Q:
    if len(q)==3:
        _,u,v = q
        if not is_all_union:
            if uf.group_count()==2 and not uf.same(u-1,v-1):
                is_all_union=True
            else:
                uf.union(u-1,v-1)
    else:
        _,v = q
        if is_all_union:
            m1_cnt+=1
        else:
            STACK.append(v-1)
for i in range(N):
    if not uf.same(0,i):
        nd = i+1
for s in STACK:
    if uf.same(0,s):
        print(nd)
    else:
        print(1)
for _ in range(m1_cnt):
    print(-1)


0