#include #define MAX 100000 int calc(int, int); int last[MAX], first[MAX], vertex[MAX * 2], next[MAX * 2], ptr, visited[MAX], ans[MAX]; int main(void) { int n, i; scanf("%d", &n); for(i = 0; i < n; i++) { first[i] = -1; } 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; vertex[ptr] = y; next[ptr] = -1; ptr++; if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; } last[y] = ptr; vertex[ptr] = x; next[ptr] = -1; ptr++; } calc(0, 0); for(i = 0; i < n; i++) { printf("%d\n", ans[i]); } return 0; } int calc(int v, int ne) { visited[v] = 1; int ha = MAX; int p = first[v]; while(p != -1) { if(visited[ vertex[p] ] == 0) { int temp = calc(vertex[p], ne + 1) + 1; ha = (temp < ha ? temp : ha); } p = next[p]; } ha = (ha == MAX ? 0 : ha); ans[v] = (ne < ha ? ne : ha); return ha; }