#include using namespace std; using i64 = int64_t; using vi = vector; using vvi = vector; vvi adj; int main() { int n; cin >> n; adj = vvi(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } vi rootdist(n, 1e9); queue que; que.push(0); rootdist[0] = 0; while (que.size()) { int t = que.front(); que.pop(); for (int s: adj[t]) { if (rootdist[s] > rootdist[t] + 1) { rootdist[s] = rootdist[t] + 1; que.push(s); } } } vi leafdist(n, 1e9); for (int i = 1; i < n; i++) { if (adj[i].size() == 1) { que.push(i); leafdist[i] = 0; } } while (que.size()) { int t = que.front(); que.pop(); for (int s: adj[t]) { if (leafdist[s] > leafdist[t] + 1) { leafdist[s] = leafdist[t] + 1; que.push(s); } } } for (int i = 0; i < n; i++) { cout << min(rootdist[i], leafdist[i]) << endl; } }