#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif namespace std { template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } } // namespace std void solve() { int N; cin >> N; vector> G(N); for (int i = 1; i < N; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } queue que; que.push(0); vector dist(N, 1e9); dist[0] = 0; y_combinator([&](auto self, int v, int p = -1) -> void { bool is_leaf = true; for(const auto&to: G[v]) if(to != p) { is_leaf = false; self(to, v); } if(is_leaf) { que.push(v); dist[v] = 0; } })(0); while(!que.empty()) { int from = que.front(); que.pop(); for(const auto&to: G[from]) { if(dist[to] > dist[from] + 1) { dist[to] = dist[from] + 1; que.push(to); } } } for(const int x: dist) cout << x << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }