結果

問題 No.872 All Tree Path
ユーザー 👑 obakyanobakyan
提出日時 2019-08-30 22:18:06
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 962 ms / 3,000 ms
コード長 1,069 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 5,464 KB
実行使用メモリ 84,152 KB
最終ジャッジ日時 2023-08-14 05:43:19
合計ジャッジ時間 8,956 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 933 ms
84,116 KB
testcase_01 AC 946 ms
84,104 KB
testcase_02 AC 938 ms
84,152 KB
testcase_03 AC 464 ms
80,988 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 948 ms
84,152 KB
testcase_06 AC 947 ms
84,032 KB
testcase_07 AC 962 ms
84,112 KB
testcase_08 AC 62 ms
9,796 KB
testcase_09 AC 64 ms
9,728 KB
testcase_10 AC 66 ms
9,800 KB
testcase_11 AC 65 ms
9,640 KB
testcase_12 AC 63 ms
9,712 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local node = {}
for i = 1, n do
  node[i] = {}
  node[i].edge, node[i].dup, node[i].asked, node[i].edgenum = {}, 1, false, 0
end
for i = 1, n - 1 do
  local u, v, w = io.read("*n", "*n", "*n")
  node[u].edge[v] = w
  node[u].edgenum = node[u].edgenum + 1
  node[v].edge[u] = w
  node[v].edgenum = node[v].edgenum + 1
end
local tasks = {}
local tasknum = 0
local done = 0
for i = 1, n do
  if node[i].edgenum == 1 then
    table.insert(tasks, i)
    tasknum = tasknum + 1
  end
end
local ret = 0LL
while done < tasknum do
  done = done + 1
  local idx = tasks[done]
  node[idx].asked = true
  for dst, len in pairs(node[idx].edge) do
    if not node[dst].asked then
      local d = node[idx].dup
      ret = ret + 1LL * d * (n - d) * len
      node[dst].edgenum = node[dst].edgenum - 1
      node[dst].dup = node[dst].dup + node[idx].dup
      if node[dst].edgenum == 1 then
        table.insert(tasks, dst)
        tasknum = tasknum + 1
      end
      break
    end
  end
end
ret = ret * 2LL
local str = tostring(ret):gsub("LL", "")
print(str)
0