#include #include #include using namespace std; int main(){ int n; cin >> n; int INF = 100001; vector> str(n+1), rev(n+1); vector r(n+1, INF), l(n+1, INF); bool notLeaf[INF] = {}; int x, y; for(int i = 0; i < n-1; i++){ cin >> x >> y; str[x].push_back(y); notLeaf[x] = true; rev[y].push_back(x); } stack> s; s.push({1, 0}); while(!s.empty()){ pair p = s.top(); s.pop(); r[p.first] = p.second; for(int next : str[p.first]){ s.push({next, p.second+1}); } } for(int i = 1; i <= n; i++){ if(notLeaf[i]) continue; s.push({i, 0}); while(!s.empty()){ pair now = s.top(); s.pop(); if(now.second >= l[now.first]) continue; l[now.first] = now.second; for(int next : rev[now.first]){ if(now.second + 1 < l[next]){ s.push({next, now.second + 1}); } } } } for(int i = 1; i <= n; i++) printf("%d\n", min(l[i], r[i])); return 0; }