結果

問題 No.1059 素敵な集合
ユーザー tpynerivertpyneriver
提出日時 2020-05-23 09:41:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,418 ms / 2,000 ms
コード長 1,006 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 357,604 KB
最終ジャッジ日時 2023-09-30 15:36:49
合計ジャッジ時間 9,487 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,468 KB
testcase_01 AC 1,418 ms
357,604 KB
testcase_02 AC 235 ms
102,860 KB
testcase_03 AC 70 ms
71,292 KB
testcase_04 AC 70 ms
71,372 KB
testcase_05 AC 71 ms
71,220 KB
testcase_06 AC 200 ms
96,792 KB
testcase_07 AC 199 ms
97,168 KB
testcase_08 AC 236 ms
105,564 KB
testcase_09 AC 139 ms
83,864 KB
testcase_10 AC 321 ms
125,624 KB
testcase_11 AC 208 ms
99,476 KB
testcase_12 AC 155 ms
89,012 KB
testcase_13 AC 317 ms
123,344 KB
testcase_14 AC 104 ms
78,600 KB
testcase_15 AC 550 ms
170,768 KB
testcase_16 AC 223 ms
101,876 KB
testcase_17 AC 209 ms
99,836 KB
testcase_18 AC 133 ms
93,588 KB
testcase_19 AC 1,394 ms
357,440 KB
testcase_20 AC 1,342 ms
331,056 KB
testcase_21 AC 484 ms
161,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

class UF():
    def __init__(self, num):
        self.par = [-1]*num
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            stack = []
            while self.par[x] >= 0:
                stack.append(x)
                x = self.par[x]
            for xi in stack:
                self.par[xi] = x
            return x
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] > self.par[ry]:
                rx, ry = ry, rx
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
            return True
        return False

L, R = map(int, readline().split())
R += 1

Edge = []
for x in range(L, R):
    for k in range(2, -(-R//x)):
        Edge.append((0, x-L, x*k-L))
for x in range(L, R-1):
    Edge.append((1, x-L, x+1-L))
T = UF(R-L)
Edge.sort()
ans = 0
for c, a, b in Edge:
    if T.union(a, b):
        ans += c
print(ans)
0