結果

問題 No.1917 LCMST
ユーザー AEnAEn
提出日時 2023-04-08 01:12:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,958 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 261,300 KB
最終ジャッジ日時 2024-04-12 10:19:22
合計ジャッジ時間 49,607 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
73,560 KB
testcase_01 AC 80 ms
73,444 KB
testcase_02 AC 73 ms
73,280 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1,624 ms
256,812 KB
testcase_30 AC 1,614 ms
257,212 KB
testcase_31 AC 1,602 ms
256,892 KB
testcase_32 AC 1,634 ms
256,812 KB
testcase_33 AC 1,690 ms
257,092 KB
testcase_34 AC 393 ms
259,340 KB
testcase_35 AC 384 ms
259,020 KB
testcase_36 AC 388 ms
259,820 KB
testcase_37 AC 1,680 ms
257,024 KB
testcase_38 WA -
testcase_39 AC 2,383 ms
257,148 KB
testcase_40 WA -
testcase_41 AC 2,304 ms
256,676 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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)

uf = UnionFind(N)

res = 0
for i in range(1,10**5+1):
    if num[i]>0:
        for j in range(num[i]-1):
            f = uf.unite(id[i][j],id[i][j+1])
            if f:
                res += i
        for j in range(2*i,10**5+1,i):
            if num[j]>0:
                for k in range(num[j]):
                    f = uf.unite(id[i][0],id[j][k])
                    if f:
                        res += j

group = uf.groups()
if len(group)==1:
    print(res)
else:
    mn = [float('inf')]*len(group)
    for i in range(len(group)):
        for g in group[i]:
            mn[i] = min(mn[i],A[g])
    mm = min(mn)
    res += mm*sum(mn)-mm*mm
    print(res)
0