結果

問題 No.826 連絡網
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-05 15:30:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,623 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 70,776 KB
最終ジャッジ日時 2023-10-13 02:21:54
合計ジャッジ時間 21,137 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,312 KB
testcase_01 AC 15 ms
8,364 KB
testcase_02 AC 18 ms
8,400 KB
testcase_03 AC 28 ms
8,560 KB
testcase_04 AC 31 ms
8,696 KB
testcase_05 AC 21 ms
8,468 KB
testcase_06 AC 21 ms
8,472 KB
testcase_07 AC 28 ms
8,528 KB
testcase_08 AC 21 ms
8,436 KB
testcase_09 AC 30 ms
8,680 KB
testcase_10 AC 18 ms
8,376 KB
testcase_11 AC 25 ms
8,624 KB
testcase_12 AC 1,574 ms
53,548 KB
testcase_13 AC 591 ms
26,264 KB
testcase_14 AC 1,125 ms
41,984 KB
testcase_15 AC 126 ms
12,192 KB
testcase_16 AC 762 ms
30,944 KB
testcase_17 AC 604 ms
26,136 KB
testcase_18 AC 469 ms
22,328 KB
testcase_19 AC 1,805 ms
60,496 KB
testcase_20 AC 1,741 ms
59,784 KB
testcase_21 AC 34 ms
8,756 KB
testcase_22 AC 623 ms
26,516 KB
testcase_23 AC 776 ms
31,496 KB
testcase_24 AC 331 ms
18,612 KB
testcase_25 TLE -
testcase_26 AC 403 ms
20,408 KB
testcase_27 AC 1,640 ms
55,136 KB
testcase_28 AC 1,232 ms
44,100 KB
testcase_29 AC 602 ms
25,956 KB
testcase_30 TLE -
testcase_31 AC 721 ms
29,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind :
    def __init__(self, size) :
        self.parent = list(range(size))
        self.height = [0] * size
        self.size = [1] * size
        self.component = size

    def root(self, index) :
        if self.parent[index] == index :  # 根の場合
            return index
        rootIndex = self.root(self.parent[index])  # 葉の場合親の根を取得
        self.parent[index] = rootIndex  # 親の付け直し
        return rootIndex

    def union(self, index1, index2) :  # 結合
        root1 = self.root(index1)
        root2 = self.root(index2)

        if root1 == root2 :  # 連結されている場合
            return

        self.component -= 1  # 連結成分を減らす

        if self.height[root1] < self.height[root2] :
            self.parent[root1] = root2  # root2に結合
            self.size[root2] += self.size[root1]
        else :
            self.parent[root2] = root1  # root1に結合
            self.size[root1] += self.size[root2]
            if self.height[root1] == self.height[root2] :
                self.height[root1] += 1
        return

    def isSameRoot(self, index1, index2) :
        return self.root(index1) == self.root(index2)

    def sizeOfSameRoot(self, index) :
        return self.size[self.root(index)]

    def getComponent(self) :
        return self.component

N, P = map(int, input().split())
tree = UnionFind(N + 1)

isPrime = [True] * (N + 1)
for i in range(2, N + 1):
    if isPrime[i]:
        for j in range(i + i, N + 1, i):
            isPrime[j] = False
            tree.union(i, j)

ans = tree.sizeOfSameRoot(P)
print(ans)
0