結果

問題 No.556 仁義なきサルたち
ユーザー AEnAEn
提出日時 2022-05-21 11:20:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 2,487 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 81,908 KB
実行使用メモリ 81,268 KB
最終ジャッジ日時 2023-10-20 15:50:50
合計ジャッジ時間 4,294 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
68,312 KB
testcase_01 AC 54 ms
68,312 KB
testcase_02 AC 56 ms
68,312 KB
testcase_03 AC 55 ms
68,312 KB
testcase_04 AC 55 ms
68,312 KB
testcase_05 AC 58 ms
68,312 KB
testcase_06 AC 55 ms
68,312 KB
testcase_07 AC 57 ms
70,504 KB
testcase_08 AC 60 ms
70,504 KB
testcase_09 AC 64 ms
72,564 KB
testcase_10 AC 79 ms
77,372 KB
testcase_11 AC 87 ms
77,544 KB
testcase_12 AC 88 ms
77,668 KB
testcase_13 AC 78 ms
78,392 KB
testcase_14 AC 133 ms
79,044 KB
testcase_15 AC 161 ms
79,804 KB
testcase_16 AC 119 ms
79,572 KB
testcase_17 AC 163 ms
80,068 KB
testcase_18 AC 235 ms
81,268 KB
testcase_19 AC 108 ms
79,312 KB
testcase_20 AC 104 ms
79,328 KB
testcase_21 AC 109 ms
79,328 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 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_count

from collections import defaultdict
b = defaultdict(int)
N, M = map(int, input().split())
for i in range(N):
    b[i] = i
uf = UnionFind(N)
for i in range(M):
    A, B = map(int, input().split())
    sa, sb = uf.size(A-1), uf.size(B-1)
    ra, rb = uf.root(A-1), uf.root(B-1)
    ba, bb = b[ra], b[rb]
    uf.unite(A-1, B-1)
    if sa == sb:
        if ba < bb:
            b[ra] = ba
            b[rb] = ba
        else:
            b[ra] = bb
            b[rb] = bb
    elif sa > sb:
        b[ra] = ba
        b[rb] = ba
    else:
        b[ra] = bb
        b[rb] = bb
for i in range(N):
    print(b[uf.root(i)]+1)
0