結果
問題 |
No.1917 LCMST
|
ユーザー |
![]() |
提出日時 | 2022-04-28 18:10:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,320 ms / 4,000 ms |
コード長 | 1,193 bytes |
コンパイル時間 | 160 ms |
コンパイル使用メモリ | 82,280 KB |
実行使用メモリ | 305,768 KB |
最終ジャッジ日時 | 2024-06-28 07:58:42 |
合計ジャッジ時間 | 30,235 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#UnionFind from collections import defaultdict 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 same(self, x, y): return self.find(x) == self.find(y) from collections import defaultdict n=int(input()) a=list(map(int,input().split())) m=10**5 c=[0]*(m+1) ans=0 for i in a: if c[i]==0: c[i]=1 else: ans+=i s=set() edge=defaultdict(list) d=[-1]*(m+1) for i in range(1,m+1): for j in range(i,m+1,i): if c[j]: if d[i]==-1: d[i]=j else: w=d[i]*j//i s.add(w) edge[w].append((d[i],j)) s=sorted(list(s)) uf=UnionFind(m+1) for w in s: for u,v in edge[w]: if not uf.same(u,v): uf.union(u,v) ans+=w print(ans)