#include #include #include #include using namespace std; #define RREP(i,s,e) for (i = s; i >= e; i--) #define rrep(i,n) RREP(i,(int)(n)-1,0) #define REP(i,s,e) for (i = s; i <= e; i++) #define rep(i,n) REP(i,0,(int)(n)-1) #define INF 100000000 typedef long long ll; vector e[100000]; int dist[100000]; void dijkstra(int x) { priority_queue> q; dist[x] = 0; q.push(make_pair(0,x)); while (!q.empty()) { int from = q.top().second; q.pop(); for (auto to : e[from]) { if (dist[to] > dist[from] + 1) { dist[to] = dist[from] + 1; q.push(make_pair(-dist[to],to)); } } } } int main() { int i, n; cin >> n; rep (i,n-1) { int x, y; cin >> x >> y; x--; y--; e[x].push_back(y); e[y].push_back(x); } rep (i,n) dist[i] = INF; dijkstra(0); REP (i,1,n) if (e[i].size() == 1) dijkstra(i); rep (i,n) cout << dist[i] << endl; return 0; }