#include #include #include #include #include using namespace std; typedef pair P; const int kMAX_N = 100010; int dim[kMAX_N]; int x[kMAX_N - 1], y[kMAX_N - 1]; int R[kMAX_N], L[kMAX_N]; int N; vector graph[kMAX_N]; void BFS(int start, int init, int cost[]) { fill(cost, cost + kMAX_N, -1); queue

que; // (頂点, 距離) cost[start] = init; que.push(P(start, init)); while (!que.empty()) { P node = que.front(); que.pop(); for (int i = 0; i < graph[node.first].size(); i++) { int to = graph[node.first][i]; if (cost[to] < 0) { cost[to] = node.second + 1; que.push(P(to, cost[to])); } } } } void Solve() { for (int i = 0; i < N - 1; i++) { printf("i %d\n", i); graph[x[i] - 1].push_back(y[i] - 1); graph[y[i] - 1].push_back(x[i] - 1); dim[x[i] - 1]++; dim[y[i] - 1]++; } int stab = N; for (int i = 0; i < N; i++) { if (i != 0 && dim[i] == 1) { // 葉 graph[i].push_back(stab); graph[stab].push_back(i); } } // 根とスタブから幅優先 BFS(0, 0, R); BFS(stab, -1, L); for (int i = 0; i < N; i++) { printf("%d\n", min(R[i], L[i])); } } int main() { scanf("%d", &N); for (int i = 0; i < N - 1; i++) { scanf("%d%d", &x[i], &y[i]); } Solve(); return 0; }