//https://ncode.syosetu.com/n4830bu/277/ #include using namespace std; int main() { int N; cin >> N; vector> G(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; x--; y--; G[x].emplace_back(y); G[y].emplace_back(x); } vector maine(N); vector seen(N); queue que; que.push(0); seen[0] = true; for (int i = 0; i < N; i++) { if (G[i].size() == 1) { que.push(i); seen[i] = true; } } while (!que.empty()) { auto idx = que.front(); que.pop(); for (auto&& nxt : G[idx]) { if (seen[nxt]) continue; seen[nxt] = true; maine[nxt] = maine[idx] + 1; que.push(nxt); } } for (auto&& x : maine) { cout << x << endl; } }