結果
| 問題 |
No.1488 Max Score of the Tree
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2021-04-30 20:09:21 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 2,000 ms |
| コード長 | 1,426 bytes |
| コンパイル時間 | 92 ms |
| コンパイル使用メモリ | 6,816 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-18 17:57:06 |
| 合計ジャッジ時間 | 1,422 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
local mmi, mma = math.min, math.max
local n, k = io.read("*n", "*n")
local edge = {}
local parent = {}
local childcnt = {}
local weight = {}
local len = {}
local base = 0
for i = 1, n do
edge[i] = {}
parent[i] = 0
childcnt[i] = 0
weight[i] = 0
len[i] = 0
end
for i = 1, n - 1 do
local a, b, c = io.read("*n", "*n", "*n")
edge[a][b] = c
edge[b][a] = c
end
local tasks = {1}
for i = 1, n do
local src = tasks[i]
for dst, l in pairs(edge[src]) do
if parent[src] ~= dst then
parent[dst] = src
len[dst] = len[src] + l
childcnt[src] = childcnt[src] + 1
table.insert(tasks, dst)
end
end
end
local edgecost, edgevalue = {}, {}
tasks = {}
for i = 1, n do
if childcnt[i] == 0 then
table.insert(tasks, i)
weight[i] = 1
base = base + len[i]
end
end
for i = 1, n - 1 do
local child = tasks[i]
local p = parent[child]
table.insert(edgecost, edge[child][p])
table.insert(edgevalue, edge[child][p] * weight[child])
weight[p] = weight[p] + weight[child]
childcnt[p] = childcnt[p] - 1
if childcnt[p] == 0 then
table.insert(tasks, p)
end
end
local dp = {}
for i = 1, k do
dp[i] = 0
end
for i = 1, n - 1 do
local c, v = edgecost[i], edgevalue[i]
for j = k - c, 1, -1 do
dp[j + c] = mma(dp[j + c], dp[j] + v)
end
if c <= k then
dp[c] = mma(dp[c], v)
end
end
local ret = 0
for i = 1, k do
ret = mma(ret, dp[i])
end
print(ret + base)