#include int x[100000] = { 0 }; int y[100000] = { 0 }; int R[100000] = { 0 }; int L[100000] = { 0 }; int N; void root_search(int depth,int now); void leaf_search(int depth, int now); int min(int a, int b); int MAX_DEPTH = 0;; int DEPTH; int main(){ scanf("%d",&N); int i; for (i = 0; i < N - 1; i++){ scanf("%d %d", &x[i], &y[i]); x[i]--; y[i]--; } root_search(0, 0); for (i = 0; i < N; i++){ DEPTH = 9999999; leaf_search(0, i); L[i] = DEPTH; } for (i = 0; i < N; i++){ printf("%d\n",min(R[i],L[i])); } return 0; } void root_search(int depth,int now){ if (MAX_DEPTH < depth){ MAX_DEPTH = depth; } R[now] = depth; int i; for (i = 0; i < N - 1; i++){ if (x[i] == now){ root_search(depth + 1, y[i]); } } } void leaf_search(int depth, int now){ if (depth>MAX_DEPTH){ return; } int i; int cnt = 0; for (i = 0; i < N - 1; i++){ if (x[i] == now){ cnt++; } } if (cnt == 0){ DEPTH = min(DEPTH, depth); return; } else{ for (i = 0; i < N - 1; i++){ if (x[i] == now){ leaf_search(depth + 1, y[i]); } } } } int min(int a, int b){ if (a >= b){ return b; } else{ return a; } }