結果

問題 No.1059 素敵な集合
ユーザー irumo8202irumo8202
提出日時 2022-01-25 16:54:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,343 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 11,108 KB
実行使用メモリ 10,644 KB
最終ジャッジ日時 2023-08-22 11:23:56
合計ジャッジ時間 7,921 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,692 KB
testcase_01 AC 1,343 ms
10,176 KB
testcase_02 AC 162 ms
9,544 KB
testcase_03 AC 19 ms
8,688 KB
testcase_04 AC 19 ms
8,664 KB
testcase_05 AC 19 ms
8,696 KB
testcase_06 AC 127 ms
9,040 KB
testcase_07 AC 125 ms
8,992 KB
testcase_08 AC 175 ms
9,392 KB
testcase_09 AC 51 ms
8,884 KB
testcase_10 AC 263 ms
9,376 KB
testcase_11 AC 140 ms
9,068 KB
testcase_12 AC 79 ms
9,060 KB
testcase_13 AC 278 ms
9,924 KB
testcase_14 AC 26 ms
8,812 KB
testcase_15 AC 524 ms
10,440 KB
testcase_16 AC 156 ms
9,408 KB
testcase_17 AC 149 ms
9,420 KB
testcase_18 AC 157 ms
10,104 KB
testcase_19 AC 1,261 ms
10,044 KB
testcase_20 AC 1,206 ms
10,308 KB
testcase_21 AC 484 ms
10,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members


L, R = map(int, input().split())
size = R - L + 1
uf = UnionFind(R + 1)

for i in range(L, R + 1):
    for j in range(i + i, R + 1, i):
        uf.union(i, j)

ans = 0
for i in range(L, R):
    if uf.same(i, i + 1):
        continue
    uf.union(i, i + 1)
    ans += 1

print(ans)
0