#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //define #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; #define dump(x) cerr << #x << " = " << (x) << endl; #define INF (INT_MAX/2) #define PI (2*acos(0.0)) #define EPS (1e-8) #define REP(i,a,b) for(int i=(a); i<(b);++i) #define rep(i,n) REP(i,0,n) typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair pll; typedef vector vint; typedef vector > vvint; typedef vector vll; typedef vector > vvll; int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int N; vector G[100001], leaf; int R[100001], L[100001]; int used[100001]; void calcR(int root){ queue que; que.push(pii(root, 0)); while(!que.empty()){ pii p = que.front(); que.pop(); int node_number = p.first; int dist_from_root = p.second; used[node_number] = 1; R[node_number] = dist_from_root; for(int i = 0; i < G[node_number].size(); i++){ int to = G[node_number][i]; if(used[to]) continue; que.push(pii(to, dist_from_root + 1)); } } } void calcL(){ for(int i = 1; i <= N; i++) L[i] = INF; for(int i = 1; i <= N; i++){ if(i != 1 && G[i].size() == 1){ L[i] = 0; leaf.push_back(i); } } for(int i = 0; i < leaf.size(); i++){ queue que; memset(used, 0, sizeof(used)); que.push(pii(leaf[i], 0)); while(!que.empty()){ pii p = que.front(); que.pop(); int node_number = p.first; int dist_from_leaf = p.second; used[node_number] = 1; if(node_number != leaf[i] && dist_from_leaf >= L[node_number]) continue; else L[node_number] = dist_from_leaf; for(int j = 0; j < G[node_number].size(); j++){ int to = G[node_number][j]; if(L[to] == 0 || used[to]) continue; que.push(pii(to, dist_from_leaf + 1)); } } } } void solve(){ calcR(1); calcL(); for(int i = 1; i <= N; i++){ cout << min(R[i], L[i]) << endl; } } int main(void){ ios_base::sync_with_stdio(0); cin >> N; for(int i = 0; i < N - 1; i++){ int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } solve(); return 0; }