結果

問題 No.1917 LCMST
ユーザー qibqib
提出日時 2023-01-06 00:05:35
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,392 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 321,048 KB
最終ジャッジ日時 2023-08-19 20:45:00
合計ジャッジ時間 14,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
76,668 KB
testcase_01 AC 78 ms
76,804 KB
testcase_02 AC 79 ms
76,700 KB
testcase_03 AC 148 ms
82,420 KB
testcase_04 AC 142 ms
82,504 KB
testcase_05 AC 145 ms
82,532 KB
testcase_06 AC 138 ms
82,260 KB
testcase_07 AC 145 ms
82,456 KB
testcase_08 AC 79 ms
76,572 KB
testcase_09 TLE -
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 #

import math

class UnionFind:
  def __init__(self, n):
    self.node = [-1 for _ in range(n)]

  def root(self, v):
    if self.node[v] < 0:
      return v

    st = []
    while self.node[v] >= 0:
      st.append(v)
      v = self.node[v]

    for u in st:
      self.node[u] = v

    return v

  def size(self, v):
    v = self.root(v)
    return (- self.node[v])

  def same(self, u, v):
    return self.root(u) == self.root(v)

  def unite(self, u, v):
    ru = self.root(u)
    rv = self.root(v)
    if ru == rv:
      return

    du = self.node[ru]
    dv = self.node[rv]
    if du <= dv:
      self.node[rv] = ru
      self.node[ru] += dv
    else:
      self.node[ru] = rv
      self.node[rv] += du

INF = 1 << 60

n = int(input())
a = list(map(int, input().split()))

grp = [[] for _ in range(100001)]
for i in range(n):
  for x in range(1, int(math.sqrt(a[i])) + 1):
    if a[i] % x == 0:
      grp[x].append(i)
      if x != a[i] // x:
        grp[a[i] // x].append(i)

edges = {}
for x in range(1, 100001):
  if len(grp[x]) < 2:
    continue
  grp[x].sort(key=lambda i: - a[i])
  u = grp[x].pop()
  for v in grp[x]:
    edges[u * n + v] = min(edges.get(u * n + v, INF), a[u] * a[v] // x)

edges = sorted(list(edges.items()), key=lambda x: x[1])
uf = UnionFind(n)
ans = 0
for e, w in edges:
  u, v = e // n, e % n
  if not uf.same(u, v):
    uf.unite(u, v)
    ans += w

print(ans)
0