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