結果

問題 No.1059 素敵な集合
ユーザー hir355hir355
提出日時 2020-05-22 22:02:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 1,077 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 11,092 KB
実行使用メモリ 27,792 KB
最終ジャッジ日時 2023-09-30 15:28:57
合計ジャッジ時間 4,320 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,276 KB
testcase_01 AC 207 ms
19,156 KB
testcase_02 AC 122 ms
13,404 KB
testcase_03 AC 16 ms
8,232 KB
testcase_04 AC 16 ms
8,272 KB
testcase_05 AC 16 ms
8,156 KB
testcase_06 AC 89 ms
11,756 KB
testcase_07 AC 88 ms
11,824 KB
testcase_08 AC 123 ms
13,152 KB
testcase_09 AC 40 ms
10,188 KB
testcase_10 AC 150 ms
13,336 KB
testcase_11 AC 92 ms
11,960 KB
testcase_12 AC 58 ms
10,912 KB
testcase_13 AC 183 ms
14,724 KB
testcase_14 AC 22 ms
8,896 KB
testcase_15 AC 324 ms
20,280 KB
testcase_16 AC 106 ms
12,616 KB
testcase_17 AC 108 ms
12,924 KB
testcase_18 AC 125 ms
27,792 KB
testcase_19 AC 442 ms
19,044 KB
testcase_20 AC 426 ms
19,912 KB
testcase_21 AC 352 ms
20,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


l, r = map(int, input().split())
uf = UnionFind(r + 1)
t = [0] * (r + 1)
for i in range(l, r + 1):
    if t[i] == 0:
        for j in range(i * 2, r + 1, i):
            t[j] = 1
            uf.unite(i, j)
ans = 0
s = set()
for i in range(l, r + 1):
    p = uf.find(i)
    if p not in s:
        ans += 1
        s.add(p)
print(len(s) - 1)
0