結果

問題 No.1639 最小通信路
ユーザー 👑 obakyan
提出日時 2021-08-06 21:54:43
言語 Lua
(LuaJit 2.1.1734355927)
結果
WA  
実行時間 -
コード長 1,248 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-17 01:52:16
合計ジャッジ時間 1,621 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local function uf_initialize(n)
  local parent = {}
  for i = 1, n do parent[i] = i end
  return parent
end

local function uf_findroot(idx, parent)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

local function kruskal(n, line)
  -- line.c, line.i1, line.i2
  table.sort(line, function(a, b) return a.c < b.c end)
  local parent = uf_initialize(n)
  local totalcost = 0
  local used = 0
  local last = 0
  local linenum = #line
  for i = 1, linenum do
    local c, i1, i2 = line[i].c, line[i].i1, line[i].i2
    local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent)
    parent[i1], parent[i2] = r1, r2
    if r1 ~= r2 then
      parent[r2] = r1
      parent[i2] = r1
      totalcost = totalcost + c
      last = c
      used = used + 1
      if used == n - 1 then break end
    end
  end
  return totalcost, last
end

local n = io.read("*n")
local line = {}
local z = mfl(n * (n - 1) / 2)
for i = 1, z do
  local t = {}
  t.i1, t.i2, t.c = io.read("*n", "*n", "*n")
  table.insert(line, t)
end
local tot, last = kruskal(n, line)
print(last)
0