// 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]; void dfs(int node, int par, int h) { int i; if (ans[node] > h) ans[node] = h; if (node && hi[node] == 1) { ans[node] = 0; if (ans[par] > 1) dfs(par, node, 1); } else { for (i = 0; i < hi[node]; i++) { if (ans[to[node][i]] > h+1) dfs(to[node][i], node, 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(0, -1, 0); for (i = 0; i < N; i++) out(ans[i]); return 0; }