結果

問題 No.1917 LCMST
ユーザー qibqib
提出日時 2022-10-31 02:22:08
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,371 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 87,200 KB
実行使用メモリ 846,772 KB
最終ジャッジ日時 2023-09-22 00:47:28
合計ジャッジ時間 7,236 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,436 KB
testcase_01 AC 69 ms
71,436 KB
testcase_02 AC 72 ms
76,344 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
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.node = [-1 for _ in range(n)]

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

  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 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[u]
    dv = self.node[v]
    if du <= dv:
      self.node[rv] = ru
      self.node[ru] += dv
    else:
      self.node[ru] = rv
      self.node[rv] += du


n = int(input())
a = list(map(int, input().split()))
vs = []
for v in range(n):
  vs.append((v, a[v]))

vs.sort(key=lambda x: x[0])
m = max(a)

par = {}
edges = []
for v, c in vs:
  if par.get(c, None) is None:
    par[c] = v
  else:
    edges.append((par[c], v, c))

for x in range(1, m + 1):
  for y in range(x, m + 1, x):
    if par.get(y, None) is None:
    	continue
    if par.get(x, None) is None:
      par[x] = par[y]
    if par[x] == par[y]:
      continue
    edges.append((par[x], par[y], a[par[x]] * a[par[y]] // x))

edges.sort(key=lambda x: x[2])
uf = UnionFind(n)
ans = 0
for u, v, w in edges:
  if not uf.same(u, v):
    uf.unite(u, v)
    ans += w

print(ans)
0