結果

問題 No.1917 LCMST
ユーザー AEnAEn
提出日時 2023-04-08 11:09:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,675 ms / 4,000 ms
コード長 2,825 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 305,188 KB
最終ジャッジ日時 2024-04-14 09:38:51
合計ジャッジ時間 75,458 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
83,328 KB
testcase_01 AC 95 ms
82,824 KB
testcase_02 AC 97 ms
82,892 KB
testcase_03 AC 150 ms
84,260 KB
testcase_04 AC 145 ms
84,224 KB
testcase_05 AC 149 ms
84,136 KB
testcase_06 AC 147 ms
84,076 KB
testcase_07 AC 149 ms
84,188 KB
testcase_08 AC 99 ms
82,816 KB
testcase_09 AC 3,340 ms
290,456 KB
testcase_10 AC 3,250 ms
290,736 KB
testcase_11 AC 3,225 ms
303,752 KB
testcase_12 AC 2,843 ms
283,520 KB
testcase_13 AC 1,116 ms
256,992 KB
testcase_14 AC 2,907 ms
289,680 KB
testcase_15 AC 772 ms
257,148 KB
testcase_16 AC 1,126 ms
256,772 KB
testcase_17 AC 1,571 ms
257,456 KB
testcase_18 AC 1,861 ms
256,820 KB
testcase_19 AC 923 ms
256,840 KB
testcase_20 AC 740 ms
256,832 KB
testcase_21 AC 983 ms
256,940 KB
testcase_22 AC 745 ms
257,200 KB
testcase_23 AC 755 ms
256,920 KB
testcase_24 AC 952 ms
257,020 KB
testcase_25 AC 742 ms
256,924 KB
testcase_26 AC 823 ms
256,896 KB
testcase_27 AC 863 ms
257,276 KB
testcase_28 AC 904 ms
257,132 KB
testcase_29 AC 3,422 ms
304,908 KB
testcase_30 AC 3,417 ms
304,664 KB
testcase_31 AC 3,374 ms
305,124 KB
testcase_32 AC 3,330 ms
305,188 KB
testcase_33 AC 3,420 ms
304,468 KB
testcase_34 AC 337 ms
257,324 KB
testcase_35 AC 337 ms
256,908 KB
testcase_36 AC 339 ms
257,104 KB
testcase_37 AC 3,374 ms
304,852 KB
testcase_38 AC 3,426 ms
304,648 KB
testcase_39 AC 3,675 ms
304,832 KB
testcase_40 AC 3,392 ms
304,520 KB
testcase_41 AC 3,386 ms
305,060 KB
testcase_42 AC 3,549 ms
305,000 KB
testcase_43 AC 407 ms
257,232 KB
testcase_44 AC 407 ms
256,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N = int(input())
A = list(map(int, input().split()))
num = [0]*(10**5+1)
id = [list() for _ in range(10**5+1)]
for i,a in enumerate(A):
    num[a] += 1
    id[a].append(i)

res = 0
l = 0
uf = UnionFind(N)
for i in range(10**5+1):
    if num[i]:
        for j in range(num[i]-1):
            uf.unite(id[i][j],id[i][j+1])
            res += i

edge = []
for i in range(1,10**5+1):
    m = float('inf')
    for j in range(i,10**5+1,i):
        if num[j]:
            if m==float('inf'):
                m = j//i
            else:
                edge.append([m*j,id[m*i][0],id[j][0]])
edge.sort()
for c,a,b in edge:
    f = uf.unite(a,b)
    if f:
        res += c
# print(uf.group_count)
print(res)
0