#include #define MAX 100000 int last[MAX], first[MAX], ans[MAX], queue[MAX], get, set; int value[MAX * 2], next[MAX * 2], ptr; int main(void) { int n, i; scanf("%d", &n); for(i = 0; i < n; i++) { first[i] = -1; ans[i] = MAX; } for(i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; if(first[x] == -1) { first[x] = ptr; } else { next[ last[x] ] = ptr; } last[x] = ptr; value[ptr] = y; next[ptr] = -1; ptr++; if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; } last[y] = ptr; value[ptr] = x; next[ptr] = -1; ptr++; } for(i = 1; i < n; i++) { if(first[i] == last[i]) { ans[i] = 0; queue[set] = i; set++; } } ans[0] = 0; queue[set] = 0; set++; while(get < set) { int v = queue[get]; int p = first[v]; while(p != -1) { if(ans[v] + 1 < ans[ value[p] ]) { ans[ value[p] ] = ans[v] + 1; queue[set] = value[p]; set++; } p = next[p]; } get++; } for(i = 0; i < n; i++) { printf("%d\n", ans[i]); } return 0; }