結果
問題 | No.277 根掘り葉掘り |
ユーザー |
👑 |
提出日時 | 2019-06-03 09:16:42 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 168 ms / 3,000 ms |
コード長 | 1,140 bytes |
コンパイル時間 | 201 ms |
コンパイル使用メモリ | 7,068 KB |
実行使用メモリ | 19,584 KB |
最終ジャッジ日時 | 2024-09-17 20:20:49 |
合計ジャッジ時間 | 3,480 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
local n = io.read("*n")local t = {}local fromroot = {}local fromleaf = {}for i = 1, n dot[i] = {}fromroot[i] = -1fromleaf[i] = -1endfor i = 1, n - 1 dolocal a, b = io.read("*n", "*n")table.insert(t[a], b)table.insert(t[b], a)endlocal tasks = {1}local tasknum, done = 1, 0fromroot[1] = 0while done < tasknum dodone = done + 1local curidx = tasks[done]for i = 1, #t[curidx] dolocal nextidx = t[curidx][i]if fromroot[nextidx] == -1 thenfromroot[nextidx] = fromroot[curidx] + 1tasknum = tasknum + 1table.insert(tasks, nextidx)endendendtasks = {}tasknum, done = 0, 0for i = 1, n doif #t[i] == 1 thentasknum = tasknum + 1table.insert(tasks, i)fromleaf[i] = 0endendwhile done < tasknum dodone = done + 1local curidx = tasks[done]for i = 1, #t[curidx] dolocal nextidx = t[curidx][i]if fromleaf[nextidx] == -1 thenfromleaf[nextidx] = fromleaf[curidx] + 1tasknum = tasknum + 1table.insert(tasks, nextidx)endendendlocal mmi = math.minfor i = 1, n doprint(mmi(fromroot[i], fromleaf[i]))end