// yukicoder: No.277 根掘り葉掘り // 2019.5.10 bal4u #include #include #include #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { int n = 0, c = gc(); do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return n; } void out(int n) { int i; char ob[20]; if (!n) pc('0'); else { i = 0; while (n) ob[i++] = n%10 + '0', n/=10; while (i--) pc(ob[i]); } pc('\n'); } #define MAX 100003 #define INF 0x33 int N; int *to[MAX], hi[MAX]; int ans[MAX]; typedef struct { int node, par, h; } Q; Q q[4*MAX]; int top; void dfs() { int i, node, par, h; q[0].node = 0, q[0].par = -1, q[0].h = 0, top = 1; while (top) { node = q[--top].node, par = q[top].par, h = q[top].h; if (ans[node] <= h) continue; ans[node] = h; if (node && hi[node] == 1) { ans[node] = 0; if (ans[par] > 1) { q[top].node = par, q[top].par = node, q[top++].h = 1; } } else { for (i = 0; i < hi[node]; i++) { if (ans[to[node][i]] <= h+1) continue; q[top].node = to[node][i], q[top].par = node, q[top++].h = h+1; } } } } int main() { int i, x, y; int *memo, sz; N = in(); memo = malloc(sizeof(int)*2*N); sz = 0; for (i = 1; i < N; i++) { memo[sz++] = x = in()-1, memo[sz++] = y = in()-1; hi[x]++, hi[y]++; } for (i = 0; i < N; i++) if (hi[i]) { to[i] = malloc(sizeof(int)*hi[i]); } memset(hi, 0, sizeof(int)*N); i = 0; while (i < sz) { x = memo[i++], y = memo[i++]; to[x][hi[x]++] = y, to[y][hi[y]++] = x; } memset(ans, INF, sizeof(int)*N); dfs(); for (i = 0; i < N; i++) out(ans[i]); return 0; }