結果

問題 No.1639 最小通信路
ユーザー 👑 obakyanobakyan
提出日時 2021-08-06 21:54:43
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 1,248 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 5,152 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 03:16:35
合計ジャッジ時間 1,681 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 10 ms
4,348 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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