結果

問題 No.1059 素敵な集合
ユーザー toyuzukotoyuzuko
提出日時 2020-05-30 15:04:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,136 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 357,760 KB
最終ジャッジ日時 2024-04-25 07:05:03
合計ジャッジ時間 12,826 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
16,128 KB
testcase_01 TLE -
testcase_02 AC 382 ms
46,944 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 32 ms
10,624 KB
testcase_06 AC 303 ms
38,712 KB
testcase_07 AC 297 ms
37,964 KB
testcase_08 AC 442 ms
50,844 KB
testcase_09 AC 102 ms
18,636 KB
testcase_10 AC 654 ms
77,056 KB
testcase_11 AC 340 ms
42,168 KB
testcase_12 AC 172 ms
25,800 KB
testcase_13 AC 703 ms
77,960 KB
testcase_14 AC 46 ms
12,288 KB
testcase_15 AC 1,344 ms
141,396 KB
testcase_16 AC 370 ms
45,364 KB
testcase_17 AC 343 ms
42,704 KB
testcase_18 AC 278 ms
37,208 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Graph():  #non-directed
    def __init__(self, n, edge, indexed=1):
        self.n = n
        self.edge = edge
        self.indexed = indexed
        self.graph = [[] for _ in range(n)]
        #for e in edge:
            #self.graph[e[0] - indexed].append((e[1] - indexed, e[2]))
            #self.graph[e[1] - indexed].append((e[0] - indexed, e[2]))

    def kruskal(self):  #UnionFind
        #edgemap = {self.edge[i]: i for i in range(len(self.edge))}
        sortedge = sorted(self.edge, key=lambda x: x[2])
        uf = UnionFind(self.n)
        res = 0
        #restored = []
        for e in sortedge:
            if uf.find(e[0] - self.indexed) != uf.find(e[1] - self.indexed):
                res += e[2]
                #restored.append(edgemap[e])
                uf.unite(e[0] - self.indexed, e[1] - self.indexed)
        return res#, restored

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        root = x
        while self.parents[root] != root:
            root = self.parents[root]
        while self.parents[x] != root:
            parent = self.parents[x]
            self.parents[x] = root
            x = parent
        return root

    def unite(self, x, y):
        xroot = self.find(x)
        yroot = self.find(y)
        if xroot == yroot: return
        xrank = self.rank[xroot]
        yrank = self.rank[yroot]
        if xrank < yrank:
            self.parents[xroot] = yroot
            self.size[yroot] += self.size[xroot]
        elif xrank == yrank:
            self.parents[yroot] = xroot
            self.rank[yroot] += 1
            self.size[xroot] += self.size[yroot]
        else:
            self.parents[yroot] = xroot
            self.size[xroot] += self.size[yroot]

L, R = map(int, input().split())

N = R - L + 1
E = []

for i in range(N - 1):
    E.append((i, i + 1, 1))

for i in range(L, R + 1):
    for j in range(2 * i, R + 1, i):
        E.append((i - L, j - L, 0))

g = Graph(N, E, 0)
print(g.kruskal())
0