結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2022-02-28 13:42:14
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,047 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 846,756 KB
最終ジャッジ日時 2023-09-10 16:52:28
合計ジャッジ時間 20,359 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,176 KB
testcase_01 AC 73 ms
71,372 KB
testcase_02 AC 76 ms
71,140 KB
testcase_03 AC 1,324 ms
152,280 KB
testcase_04 AC 1,303 ms
152,280 KB
testcase_05 AC 1,313 ms
152,420 KB
testcase_06 AC 1,333 ms
152,548 KB
testcase_07 AC 1,318 ms
152,364 KB
testcase_08 AC 73 ms
71,404 KB
testcase_09 MLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

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

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

def MST(n,edge):
  uf=UnionFind(n)
  ans=0
  for cost,u,v in edge:
    if not uf.same(u,v):
      ans+=cost
      uf.union(u,v)
  return ans

import math
n=int(input())
a=list(map(int,input().split()))
edge=[]
for i in range(n):
  for j in range(i+1,n):
    lcm=a[i]*a[j]//math.gcd(a[i],a[j])
    edge.append((lcm,i,j))

edge.sort(key=lambda x:x[0])
print(MST(n,edge))
0