結果

問題 No.1059 素敵な集合
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-08 20:59:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 81,532 KB
実行使用メモリ 78,776 KB
最終ジャッジ日時 2023-10-18 22:40:47
合計ジャッジ時間 3,166 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,380 KB
testcase_01 AC 78 ms
69,260 KB
testcase_02 AC 125 ms
77,344 KB
testcase_03 AC 38 ms
53,380 KB
testcase_04 AC 37 ms
53,380 KB
testcase_05 AC 38 ms
53,380 KB
testcase_06 AC 122 ms
77,048 KB
testcase_07 AC 111 ms
76,476 KB
testcase_08 AC 111 ms
76,896 KB
testcase_09 AC 107 ms
75,872 KB
testcase_10 AC 114 ms
76,780 KB
testcase_11 AC 109 ms
76,388 KB
testcase_12 AC 115 ms
76,220 KB
testcase_13 AC 124 ms
77,276 KB
testcase_14 AC 59 ms
68,180 KB
testcase_15 AC 130 ms
78,396 KB
testcase_16 AC 109 ms
76,556 KB
testcase_17 AC 116 ms
76,680 KB
testcase_18 AC 54 ms
67,148 KB
testcase_19 AC 116 ms
78,744 KB
testcase_20 AC 154 ms
78,776 KB
testcase_21 AC 135 ms
78,572 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