#include #include template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; void solve() { int n; std::cin >> n; Graph<> graph(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; graph.span(false, --u, --v); } std::vector ans(n); auto dfs = [&](auto&& f, int v, int p, int d) -> int { int c = n; for (auto e : graph[v]) { int u = e.dst; if (u == p) continue; c = std::min(c, f(f, u, v, d + 1) + 1); } if (c == n) c = 0; // leaf ans[v] = std::min(d, c); return c; }; dfs(dfs, 0, -1, 0); for (auto x : ans) std::cout << x << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }