#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);//0も葉とみなして辺を張り替えた後の葉との最短距離 dp2[0] = 0; for (int v : bfs) { int deg = c[v].size(); vector left(deg + 1, INF); vector right(deg + 2, INF); for (int i = 0; i < deg; i++) { int w = c[v][i]; left[i + 1] = min(left[i], dp1[w]); } for (int i = deg - 1; i >= 0; i--) { int w = c[v][i]; right[i] = min(right[i + 1], dp1[w]); } for (int i = 0; i < deg; i++) { int w = c[v][i]; int tmp = min(left[i], right[i + 1]); tmp++; tmp = min(tmp, dp2[v]); tmp++; dp2[w] = tmp; } } for (int i = 0; i < N; i++) { cout << min(dp1[i], dp2[i]) << endl; } }