結果

問題 No.826 連絡網
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-05 15:31:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 1,623 bytes
コンパイル時間 417 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 108,604 KB
最終ジャッジ日時 2023-10-13 02:22:01
合計ジャッジ時間 6,440 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,436 KB
testcase_01 AC 75 ms
71,108 KB
testcase_02 AC 79 ms
75,264 KB
testcase_03 AC 101 ms
77,296 KB
testcase_04 AC 105 ms
77,652 KB
testcase_05 AC 97 ms
77,376 KB
testcase_06 AC 97 ms
76,960 KB
testcase_07 AC 102 ms
77,180 KB
testcase_08 AC 99 ms
77,368 KB
testcase_09 AC 105 ms
77,556 KB
testcase_10 AC 89 ms
76,492 KB
testcase_11 AC 101 ms
77,440 KB
testcase_12 AC 209 ms
99,936 KB
testcase_13 AC 140 ms
86,408 KB
testcase_14 AC 178 ms
94,184 KB
testcase_15 AC 112 ms
79,704 KB
testcase_16 AC 151 ms
88,616 KB
testcase_17 AC 141 ms
86,364 KB
testcase_18 AC 133 ms
84,456 KB
testcase_19 AC 244 ms
103,528 KB
testcase_20 AC 234 ms
103,256 KB
testcase_21 AC 105 ms
77,820 KB
testcase_22 AC 142 ms
86,796 KB
testcase_23 AC 154 ms
89,108 KB
testcase_24 AC 124 ms
82,612 KB
testcase_25 AC 258 ms
108,604 KB
testcase_26 AC 128 ms
83,404 KB
testcase_27 AC 211 ms
100,892 KB
testcase_28 AC 182 ms
95,604 KB
testcase_29 AC 138 ms
86,500 KB
testcase_30 AC 265 ms
108,372 KB
testcase_31 AC 148 ms
88,072 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