#include using namespace std; const int INF = 1e9; int main() { int N; cin >> N; vector> E(N); for (int i = 0; i < N - 1; i++) { int A, B; cin >> A >> B; A--; B--; E[A].push_back(B); E[B].push_back(A); } vector> c(N); vector p(N, -1); vector bfs; queue Q; Q.push(0); while (!Q.empty()) { int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]) { if (w != p[v]) { p[w] = v; c[v].push_back(w); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector dp1(N, INF);//L for (int v : bfs) { if (c[v].size() == 0) { dp1[v] = 0; } for (int w : c[v]) { dp1[v] = min(dp1[v], dp1[w] + 1); } } reverse(bfs.begin(), bfs.end()); vector dp2(N, INF);//R dp2[0] = 0; for (int v : bfs) { for (int w : c[v]) { dp2[w] = min(dp2[w], dp2[v] + 1); } } for (int i = 0; i < N; i++) { cout << min(dp1[i], dp2[i]) << endl;; } }