#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   using namespace std;   inline int read() {     int x = 0, neg = 1;     char op = getchar();     while (!isdigit(op)) {       if (op ==  - ) neg = -1;       op = getchar();     }     while (isdigit(op)) {       x = 10 * x + op -  0 ;       op = getchar();     }     return neg * x;   }   inline void print(int x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x >= 10) print(x / 10);     putchar(x % 10 +  0 );   }   const int N = 1000005;   vector<int> adj[N];   void add(int u, int v) { adj[u].push_back(v); }   int n;   int heavy[N], len[N];   void dfs1(int u, int fa) {     for (auto v : adj[u]) {       if (v == fa) continue;       dfs1(v, u);       if (len[v] > len[heavy[u]]) heavy[u] = v;     }     len[u] = len[heavy[u]] + 1;   }   int *f[N], tmp[N], *id = tmp, ans[N];   void dfs2(int u, int fa) {     f[u][0] = 1;     if (heavy[u]) {       f[heavy[u]] = f[u] + 1;       dfs2(heavy[u], u);       ans[u] = ans[heavy[u]] + 1;     }     for (auto v : adj[u]) {       if (v == fa || v == heavy[u]) continue;       f[v] = id, id += len[v];       dfs2(v, u);       for (register int j = 1; j <= len[v]; j++) {         f[u][j] += f[v][j - 1];         if ((j < ans[u] && f[u][j] >= f[u][ans[u]]) ||             (j > ans[u] && f[u][j] > f[u][ans[u]])) {           ans[u] = j;         }       }     }     if (f[u][ans[u]] == 1) {       ans[u] = 0;     }   }   int main() {     n = read();     for (register int i = 1; i < n; i++) {       int u = read(), v = read();       add(u, v), add(v, u);     }     int root = 1;     dfs1(root, 0);     f[1] = id, id += len[1];     dfs2(root, 0);     for (register int i = 1; i <= n; i++) {       printf( %d n , ans[i]);     }     return 0;   }   