結果

問題 No.1059 素敵な集合
ユーザー toyuzukotoyuzuko
提出日時 2020-05-30 15:06:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,244 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 370,412 KB
最終ジャッジ日時 2023-09-30 15:38:44
合計ジャッジ時間 8,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,088 KB
testcase_01 AC 1,244 ms
370,412 KB
testcase_02 AC 328 ms
107,216 KB
testcase_03 AC 72 ms
71,308 KB
testcase_04 AC 71 ms
71,188 KB
testcase_05 AC 72 ms
71,128 KB
testcase_06 AC 189 ms
99,764 KB
testcase_07 AC 191 ms
99,920 KB
testcase_08 AC 221 ms
108,924 KB
testcase_09 AC 140 ms
84,060 KB
testcase_10 AC 300 ms
127,024 KB
testcase_11 AC 201 ms
102,496 KB
testcase_12 AC 156 ms
90,352 KB
testcase_13 AC 289 ms
126,632 KB
testcase_14 AC 105 ms
78,864 KB
testcase_15 AC 458 ms
178,824 KB
testcase_16 AC 210 ms
105,532 KB
testcase_17 AC 206 ms
103,368 KB
testcase_18 AC 142 ms
96,912 KB
testcase_19 AC 1,153 ms
366,892 KB
testcase_20 AC 1,134 ms
338,772 KB
testcase_21 AC 448 ms
170,004 KB
権限があれば一括ダウンロードができます

ソースコード

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:
        for e in self.edge:
            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(L, R + 1):
    for j in range(2 * i, R + 1, i):
        E.append((i - L, j - L, 0))

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

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