結果

問題 No.826 連絡網
ユーザー AEnAEn
提出日時 2022-06-17 01:23:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 923 ms / 2,000 ms
コード長 2,106 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 94,416 KB
最終ジャッジ日時 2024-04-16 15:18:02
合計ジャッジ時間 10,885 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
67,836 KB
testcase_01 AC 63 ms
67,228 KB
testcase_02 AC 73 ms
73,504 KB
testcase_03 AC 98 ms
78,460 KB
testcase_04 AC 99 ms
78,668 KB
testcase_05 AC 99 ms
79,036 KB
testcase_06 AC 97 ms
78,340 KB
testcase_07 AC 99 ms
79,012 KB
testcase_08 AC 96 ms
78,780 KB
testcase_09 AC 98 ms
78,908 KB
testcase_10 AC 86 ms
78,388 KB
testcase_11 AC 96 ms
78,732 KB
testcase_12 AC 606 ms
89,716 KB
testcase_13 AC 270 ms
82,952 KB
testcase_14 AC 455 ms
86,940 KB
testcase_15 AC 129 ms
79,900 KB
testcase_16 AC 333 ms
84,368 KB
testcase_17 AC 277 ms
83,160 KB
testcase_18 AC 230 ms
82,308 KB
testcase_19 AC 760 ms
91,944 KB
testcase_20 AC 757 ms
91,404 KB
testcase_21 AC 103 ms
78,952 KB
testcase_22 AC 273 ms
83,304 KB
testcase_23 AC 331 ms
84,704 KB
testcase_24 AC 187 ms
81,116 KB
testcase_25 AC 923 ms
94,416 KB
testcase_26 AC 212 ms
81,832 KB
testcase_27 AC 645 ms
90,592 KB
testcase_28 AC 495 ms
87,496 KB
testcase_29 AC 274 ms
83,124 KB
testcase_30 AC 891 ms
94,280 KB
testcase_31 AC 317 ms
84,316 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

N, P = map(int, input().split())
uf = UnionFind(N)
n = [i+1 for i in range(N)]
import math
for i in range(2,N+1):
    for j in range(i, N+1, i):
        if math.gcd(i, j) != 1:
            uf.unite(i-1, j-1)
print(uf.size(uf.root(P-1)))
0