結果

問題 No.1059 素敵な集合
ユーザー ayaoniayaoni
提出日時 2020-11-22 11:26:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 970 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 93,860 KB
最終ジャッジ日時 2023-09-30 22:37:57
合計ジャッジ時間 4,572 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,096 KB
testcase_01 AC 133 ms
80,844 KB
testcase_02 AC 153 ms
80,884 KB
testcase_03 AC 75 ms
71,212 KB
testcase_04 AC 72 ms
71,180 KB
testcase_05 AC 74 ms
71,408 KB
testcase_06 AC 140 ms
78,880 KB
testcase_07 AC 142 ms
78,760 KB
testcase_08 AC 143 ms
80,084 KB
testcase_09 AC 137 ms
78,160 KB
testcase_10 AC 136 ms
79,468 KB
testcase_11 AC 140 ms
79,516 KB
testcase_12 AC 145 ms
78,712 KB
testcase_13 AC 148 ms
81,148 KB
testcase_14 AC 86 ms
76,492 KB
testcase_15 AC 163 ms
83,608 KB
testcase_16 AC 142 ms
79,308 KB
testcase_17 AC 141 ms
80,156 KB
testcase_18 AC 94 ms
93,860 KB
testcase_19 AC 146 ms
81,880 KB
testcase_20 AC 183 ms
82,564 KB
testcase_21 AC 161 ms
84,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x


L,R = MI()
UF = UnionFind(R)
for i in range(L,R//2+1):
    for j in range(2*i,R+1,i):
        UF.unite(i,j)

X = [UF.find(i) for i in range(L,R+1)]
print(len(set(X))-1)
0