結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,716 KB
testcase_01 AC 1,353 ms
10,096 KB
testcase_02 AC 163 ms
9,572 KB
testcase_03 AC 20 ms
8,616 KB
testcase_04 AC 19 ms
8,576 KB
testcase_05 AC 19 ms
8,692 KB
testcase_06 AC 126 ms
9,052 KB
testcase_07 AC 123 ms
9,120 KB
testcase_08 AC 179 ms
9,280 KB
testcase_09 AC 52 ms
8,916 KB
testcase_10 AC 263 ms
9,212 KB
testcase_11 AC 140 ms
8,972 KB
testcase_12 AC 81 ms
8,984 KB
testcase_13 AC 279 ms
9,836 KB
testcase_14 AC 27 ms
8,768 KB
testcase_15 AC 512 ms
10,268 KB
testcase_16 AC 152 ms
9,364 KB
testcase_17 AC 146 ms
9,272 KB
testcase_18 AC 157 ms
10,176 KB
testcase_19 AC 1,230 ms
10,048 KB
testcase_20 AC 1,197 ms
10,380 KB
testcase_21 AC 487 ms
10,528 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