結果

問題 No.277 根掘り葉掘り
ユーザー 👑 obakyanobakyan
提出日時 2019-06-03 09:16:42
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 199 ms / 3,000 ms
コード長 1,140 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 19,488 KB
最終ジャッジ日時 2023-10-17 23:13:31
合計ジャッジ時間 4,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 147 ms
17,864 KB
testcase_10 AC 143 ms
18,864 KB
testcase_11 AC 180 ms
19,488 KB
testcase_12 AC 181 ms
18,648 KB
testcase_13 AC 199 ms
18,928 KB
testcase_14 AC 179 ms
18,752 KB
testcase_15 AC 176 ms
18,504 KB
testcase_16 AC 184 ms
18,780 KB
testcase_17 AC 174 ms
18,532 KB
testcase_18 AC 169 ms
18,468 KB
testcase_19 AC 171 ms
18,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local t = {}
local fromroot = {}
local fromleaf = {}
for i = 1, n do
  t[i] = {}
  fromroot[i] = -1
  fromleaf[i] = -1
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(t[a], b)
  table.insert(t[b], a)
end

local tasks = {1}
local tasknum, done = 1, 0
fromroot[1] = 0
while done < tasknum do
  done = done + 1
  local curidx = tasks[done]
  for i = 1, #t[curidx] do
    local nextidx = t[curidx][i]
    if fromroot[nextidx] == -1 then
      fromroot[nextidx] = fromroot[curidx] + 1
      tasknum = tasknum + 1
      table.insert(tasks, nextidx)
    end
  end
end

tasks = {}
tasknum, done = 0, 0
for i = 1, n do
  if #t[i] == 1 then
    tasknum = tasknum + 1
    table.insert(tasks, i)
    fromleaf[i] = 0
  end
end

while done < tasknum do
  done = done + 1
  local curidx = tasks[done]
  for i = 1, #t[curidx] do
    local nextidx = t[curidx][i]
    if fromleaf[nextidx] == -1 then
      fromleaf[nextidx] = fromleaf[curidx] + 1
      tasknum = tasknum + 1
      table.insert(tasks, nextidx)
    end
  end
end
local mmi = math.min
for i = 1, n do
  print(mmi(fromroot[i], fromleaf[i]))
end
0