/** * @FileName a.cpp * @Author kanpurin * @Created 2020.05.28 01:55:18 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; vector dist1,dist2; vector> g; // 根からの距離 void dfs1(int v, int d = 0,int p = -1) { dist1[v] = d; for(int u : g[v]) { if (p == u) continue; dfs1(u, d + 1, v); } } // 葉からの距離 int dfs2(int v,int p = -1) { int leaf = 1000000; for(int u : g[v]) { if (p == u) continue; leaf = min(leaf,dfs2(u, v) + 1); } if (leaf == 1000000) { dist2[v] = 0; } else { dist2[v] = leaf; } return dist2[v]; } int main() { int n;cin >> n; g.resize(n); dist1.resize(n); dist2.resize(n); for (int i = 0; i < n-1; i++) { int u,v;cin >> u >> v; u--;v--; g[u].push_back(v); g[v].push_back(u); } dfs1(0); dfs2(0); for (int i = 0; i < n; i++) { cout << min(dist1[i],dist2[i]) << endl; } return 0; }