結果

問題 No.1059 素敵な集合
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-08 20:59:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,617 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 12,064 KB
実行使用メモリ 13,620 KB
最終ジャッジ日時 2023-10-18 22:40:22
合計ジャッジ時間 9,289 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,196 KB
testcase_01 AC 1,617 ms
13,300 KB
testcase_02 AC 177 ms
11,772 KB
testcase_03 AC 29 ms
10,196 KB
testcase_04 AC 29 ms
10,196 KB
testcase_05 AC 29 ms
10,196 KB
testcase_06 AC 142 ms
11,220 KB
testcase_07 AC 140 ms
11,164 KB
testcase_08 AC 195 ms
11,660 KB
testcase_09 AC 61 ms
10,640 KB
testcase_10 AC 303 ms
11,616 KB
testcase_11 AC 158 ms
11,232 KB
testcase_12 AC 90 ms
10,920 KB
testcase_13 AC 304 ms
12,224 KB
testcase_14 AC 36 ms
10,284 KB
testcase_15 AC 587 ms
13,328 KB
testcase_16 AC 171 ms
11,464 KB
testcase_17 AC 158 ms
11,600 KB
testcase_18 AC 157 ms
13,300 KB
testcase_19 AC 1,537 ms
13,300 KB
testcase_20 AC 1,493 ms
13,436 KB
testcase_21 AC 545 ms
13,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

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

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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


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

for i in range(L, R + 1):
    v = i * 2
    while v <= R:
        uf.unite(i, v)
        v += i

cost = 0
for i in range(L, R):
    if uf.isSame(i, i + 1):
        continue
    uf.unite(i, i + 1)
    cost += 1

print(cost)
0