結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー 👑 obakyanobakyan
提出日時 2021-04-20 23:28:31
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 807 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 28,532 KB
最終ジャッジ日時 2023-09-17 08:58:20
合計ジャッジ時間 8,022 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 19 ms
4,380 KB
testcase_06 AC 23 ms
4,928 KB
testcase_07 AC 12 ms
4,380 KB
testcase_08 AC 24 ms
5,176 KB
testcase_09 AC 21 ms
4,928 KB
testcase_10 AC 19 ms
4,380 KB
testcase_11 AC 17 ms
4,380 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 312 ms
24,696 KB
testcase_15 AC 291 ms
24,156 KB
testcase_16 AC 265 ms
23,180 KB
testcase_17 AC 170 ms
15,632 KB
testcase_18 AC 343 ms
25,792 KB
testcase_19 AC 395 ms
27,888 KB
testcase_20 AC 411 ms
27,828 KB
testcase_21 AC 403 ms
27,944 KB
testcase_22 AC 411 ms
27,864 KB
testcase_23 AC 378 ms
27,784 KB
testcase_24 AC 401 ms
27,860 KB
testcase_25 AC 402 ms
27,952 KB
testcase_26 AC 403 ms
27,864 KB
testcase_27 AC 415 ms
27,844 KB
testcase_28 AC 408 ms
27,872 KB
testcase_29 AC 183 ms
26,500 KB
testcase_30 AC 182 ms
26,472 KB
testcase_31 AC 207 ms
28,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local asked = {}
for i = 1, n do
  asked[i] = false
end
asked[1] = true
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if not asked[dst] then
      table.insert(tasks, dst)
      asked[dst] = true
    end
  end
end
local spos = tasks[n]
local len = {}
for i = 1, n do
  len[i] = -1
end
len[spos] = 0
tasks = {spos}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if len[dst] < 0 then
      len[dst] = len[src] + 1
      table.insert(tasks, dst)
    end
  end
end
print(2 * (n - 1) - len[tasks[n]])
0