#include #include #include using namespace std; using i64 = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x> G; int main(void) { int n; scanf("%d", &n); G.assign(n, vector()); for(int i : range(n-1)) { int x, y; scanf("%d%d", &x, &y); x--, y--; G[x].push_back(y); G[y].push_back(x); } vector dist(n, inf); dist[0] = 0; queue que; que.push(0); for(int v : range(1, n)) { if(G[v].size() == 1) { dist[v] = 0; que.push(v); } } while(!que.empty()) { int v = que.front(); que.pop(); for(int vv : G[v]) { if(dist[vv] > dist[v] + 1) { dist[vv] = dist[v] + 1; que.push(vv); } } } for(int v : range(n)) { printf("%d\n", dist[v]); } return 0; }