結果

問題 No.2290 UnUnion Find
ユーザー yassu0320yassu0320
提出日時 2023-05-06 17:54:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 812 ms / 2,000 ms
コード長 3,282 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 116,224 KB
最終ジャッジ日時 2024-11-24 00:38:09
合計ジャッジ時間 35,848 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
79,568 KB
testcase_01 AC 107 ms
79,520 KB
testcase_02 AC 491 ms
91,664 KB
testcase_03 AC 575 ms
97,712 KB
testcase_04 AC 596 ms
97,648 KB
testcase_05 AC 586 ms
97,824 KB
testcase_06 AC 593 ms
97,792 KB
testcase_07 AC 605 ms
97,764 KB
testcase_08 AC 604 ms
97,940 KB
testcase_09 AC 598 ms
97,796 KB
testcase_10 AC 606 ms
97,832 KB
testcase_11 AC 606 ms
97,704 KB
testcase_12 AC 608 ms
97,656 KB
testcase_13 AC 599 ms
97,716 KB
testcase_14 AC 602 ms
97,560 KB
testcase_15 AC 594 ms
97,856 KB
testcase_16 AC 598 ms
97,564 KB
testcase_17 AC 602 ms
97,716 KB
testcase_18 AC 612 ms
97,724 KB
testcase_19 AC 702 ms
99,824 KB
testcase_20 AC 763 ms
116,224 KB
testcase_21 AC 552 ms
91,680 KB
testcase_22 AC 619 ms
92,916 KB
testcase_23 AC 632 ms
93,056 KB
testcase_24 AC 745 ms
106,540 KB
testcase_25 AC 654 ms
91,748 KB
testcase_26 AC 746 ms
112,060 KB
testcase_27 AC 734 ms
107,520 KB
testcase_28 AC 664 ms
96,076 KB
testcase_29 AC 725 ms
103,908 KB
testcase_30 AC 557 ms
91,476 KB
testcase_31 AC 733 ms
102,012 KB
testcase_32 AC 595 ms
92,736 KB
testcase_33 AC 657 ms
96,088 KB
testcase_34 AC 770 ms
115,712 KB
testcase_35 AC 812 ms
109,824 KB
testcase_36 AC 632 ms
92,316 KB
testcase_37 AC 675 ms
95,740 KB
testcase_38 AC 632 ms
92,032 KB
testcase_39 AC 659 ms
92,820 KB
testcase_40 AC 768 ms
107,372 KB
testcase_41 AC 616 ms
91,520 KB
testcase_42 AC 732 ms
99,664 KB
testcase_43 AC 715 ms
99,636 KB
testcase_44 AC 635 ms
97,796 KB
testcase_45 AC 640 ms
97,792 KB
testcase_46 AC 658 ms
97,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

from pprint import pprint
from string import ascii_lowercase as letter
from sys import setrecursionlimit, stderr, stdin
from typing import Dict, Generic, Iterable, Iterator, List, Optional, Set, TypeVar, Union

try:
    import pypyjit
    pypyjit.set_param('max_unroll_recursion=-1')
except ModuleNotFoundError:
    ...

INF: int = (1 << 62) - 1
MOD1000000007 = 10**9 + 7
MOD998244353 = 998244353
setrecursionlimit(500_000)  # 5*10**5
readline = stdin.readline
input = lambda: stdin.readline().rstrip('\r\n')


def copy2d(a: list) -> list:
    return [[y for y in x] for x in a]


def copy3d(a: list) -> list:
    return [[[z for z in y] for y in x] for x in a]


def flattern2d(mat: list) -> list:
    return [x for a in mat for x in a]


def debug(*a):
    pprint(a, stream=stderr)


def inputs(type_=int, one_word=False) -> list:
    in_ = input()
    if one_word:
        ins = list(in_)
    else:
        ins = in_.split()

    if isinstance(type_, Iterable):
        return [t(x) for t, x in zip(type_, ins)]
    else:
        return list(map(type_, ins))


def input_iter(size, type_: type = int, one_word: bool = False):
    for _ in range(size):
        yield inputs(type_=type_, one_word=one_word)


def inputi() -> int:
    return int(readline())


yn = ['no', 'yes']
Yn = ['No', 'Yes']
YN = ['NO', 'YES']


# start coding
# {{{ UnionFind
class UnionFind:
    """
    Ref: github.com/cheran-senthil/PyRival/blob/master/pyrival/data_structures/DisjointSetUnion.py
    """

    def __init__(self, n):
        self.parent = list(range(n))
        self.n = n
        self._size = [1] * n
        self.num_groups = n

    def find(self, a: int) -> int:
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a: int, b: int) -> int:
        a, b = self.find(a), self.find(b)
        if a == b:
            return a

        if self._size[a] < self._size[b]:
            a, b = b, a

        self.num_groups -= 1
        self.parent[b] = a
        self._size[a] += self._size[b]

        return a

    def same(self, a: int, b: int) -> bool:
        return self.find(a) == self.find(b)

    def size(self, a) -> int:
        return self._size[self.find(a)]

    def groups(self):
        """
        計算量: O(n * log(n) * alpha(n))
        """
        gs = dict()
        for u in range(self.n):
            p = self.find(u)
            if p not in gs:
                gs[p] = []
            gs[p].append(u)

        return list(gs.values())

    def __len__(self):
        return self.num_groups


# }}}
n, q = inputs()
uf = UnionFind(n)
s = set()
for u in range(n):
    s.add(u)

for _ in range(q):
    t, *args = inputs()
    if t == 1:
        u, v = args
        u -= 1
        v -= 1
        r = uf.find(u)
        s.discard(r)
        r = uf.find(v)
        s.discard(r)
        r = uf.union(u, v)
        s.add(r)
    else:
        if len(s) == 1:
            print(-1)
            continue
        v, = args
        v -= 1
        r1, r2 = s.pop(), s.pop()
        if uf.find(v) == r1:
            print(r2 + 1)
        else:
            print(r1 + 1)
        s.add(r1)
        s.add(r2)
0