#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include using namespace std; typedef long long ll; //#define int ll //#define endl "\n" typedef vector vi; typedef vector vvi; typedef pair pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i ostream & operator<<(ostream & os, vector const &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple const &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple const & t){ os << (n==0?"":" ") << get(t); _ot(os, t); } template ostream & operator<<(ostream & os, tuple const & t){ _ot<0>(os, t); return os; } template ostream & operator<<(ostream & os, pair const & p){ return os << "(" << p.first << ", " << p.second << ") "; } template ostream & operator<<(ostream & os, vector const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } template inline bool chmax(T & x, T const & y){ return x inline bool chmin(T & x, T const & y){ return x>y ? x=y,true : false; } #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "< f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; int const inf = 1e8; vector dijkstra(Graph const & g, int s) { typedef tuple State; priority_queue q; vector dist(g.size(), inf); dist[s] = 0; q.emplace(0,s); while (q.size()) { Weight d; int v; tie(d,v) = q.top(); q.pop(); d *= -1; /* if(v == t) return d; */ if (dist[v] < d) continue; rep(i,g[v].size()){ const Edge &e = g[v][i]; if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } return dist; } signed main(){ int N; cin >> N; Graph g(N+1); rep(i,N-1){ int a,b; cin >> a >> b; a--, b--; g[a].eb(a,b,1); g[b].eb(b,a,1); } int s = N; rep(i,N) if(g[i].size()==1) g[s].eb(s,i,0); g[s].eb(s,0,0); vi d = dijkstra(g,s); rep(i,N) cout << d[i] << endl; }