func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   namespace SBT {   const int _N = N << 2 | 1;   int lc[_N], rc[_N], siz[_N], val[_N], rp[_N], rsiz[_N];   int tp, dl[_N], tot, q[_N], qc[_N];   const double alpha = 0.55;   inline int _new(int v, int ct) {     int u = tp ? dl[tp--] : ++tot;     lc[u] = rc[u] = 0, val[u] = v, siz[u] = rp[u] = ct, rsiz[u] = 1;     return u;   }   inline void upt(int x) {     siz[x] = siz[lc[x]] + siz[rc[x]] + rp[x];     rsiz[x] = rsiz[lc[x]] + rsiz[rc[x]] + 1;   }   inline void zig(int &u) {     int v = lc[u];     lc[u] = rc[v];     rc[v] = u;     upt(u);     upt(v);     u = v;   }   inline void zag(int &u) {     int v = rc[u];     rc[u] = lc[v];     lc[v] = u;     upt(u);     upt(v);     u = v;   }   inline void ins(int &u, int v, int ct = 1) {     if (!u) {       u = _new(v, ct);       return;     }     siz[u] += ct;     if (val[u] == v)       rp[u] += ct;     else if (v < val[u]) {       ins(lc[u], v, ct);       if (rsiz[lc[u]] > rsiz[u] * alpha) zig(u);     } else {       ins(rc[u], v, ct);       if (rsiz[rc[u]] > rsiz[u] * alpha) zag(u);     }     upt(u);   }   inline void del(int &u, int v) {     if (!u) return;     if (val[u] == v) {       if (rp[u] > 1)         --rp[u], --siz[u];       else if (!lc[u] || !rc[u])         dl[++tp] = u, u = lc[u] | rc[u];       else if (rsiz[lc[u]] > rsiz[rc[u]])         zig(u), del(u, v);       else         zag(u), del(u, v);       if (u) upt(u);       return;     }     if (v < val[u])       del(lc[u], v);     else       del(rc[u], v);     upt(u);   }   int rank(int u, int v) {     if (!u) return 0;     if (val[u] == v) return siz[lc[u]] + rp[u];     if (v < val[u]) return rank(lc[u], v);     return rank(rc[u], v) + siz[lc[u]] + rp[u];   }   inline int range(int u, int l, int r) { return rank(u, r) - rank(u, l - 1); }   void us_dfs(int u) {     if (lc[u]) us_dfs(lc[u]);     q[++q[0]] = val[u], qc[q[0]] = rp[u];     dl[++tp] = u;     if (rc[u]) us_dfs(rc[u]);   }   inline void mge(int &u, int &v) {     if (rsiz[u] < rsiz[v]) swap(u, v);     q[0] = 0;     us_dfs(v);     v = 0;     for (int i = 1; i <= q[0]; ++i) ins(u, q[i], qc[i]);   }   }  // namespace SBT   using SBT::del;   using SBT::ins;   using SBT::mge;   using SBT::range;   int n, root, tot;   int fi[N], ne[N], to[N];   int fa[N], siz[N], ans[N];   inline int read() {     int s = 0, w = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) w = -1;     for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48);     return s * w;   }   void print(int x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) print(x / 10);     putchar(x % 10 +  0 );   }   inline void add(int x, int y) {     ne[++tot] = fi[x], fi[x] = tot, to[tot] = y;     ne[++tot] = fi[y], fi[y] = tot, to[tot] = x;   }   void pre_dfs(int x) {     siz[x] = 1;     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])       if (v != fa[x]) {         pre_dfs(v);         siz[x] += siz[v];       }   }   int Rt, rt_path, rt[N];   inline bool check(int u, int mn, int mx, int maxson, int lim) {     int l = mx - lim, r = lim - mn;     if (l > r) return false;     if (maxson != fa[u]) return range(rt[maxson], l, r) > 0;     int s1 = range(Rt, l, r);     int s2 = range(rt[u], l, r);     int s3 = range(rt_path, l + siz[u], r + siz[u]);     return s1 + s3 > s2;   }   inline void solve_dfs(int x) {     del(Rt, siz[x]);     if (x != root) ins(rt_path, siz[x]);     int mn = 1e9, mx = 0, submx = 0, mxson = 0;     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) {       if (v != fa[x]) solve_dfs(v);       int sz = v == fa[x] ? n - siz[x] : siz[v];       mn = min(mn, sz);       if (sz > mx) {         submx = mx, mx = sz, mxson = v;       } else if (sz > submx)         submx = sz;     }     ins(Rt, siz[x]);     ins(rt[x], siz[x]);     if (x != root) del(rt_path, siz[x]);     if (mxson == fa[x])       for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])         if (v != fa[x]) mge(rt[x], rt[v]);     int l = submx == 0 ? mx : submx, r = mx, mid = 0;     while (l < r)       check(x, mn, mx, mxson, mid = l + r >> 1) ? r = mid : l = mid + 1;     ans[x] = l;     if (mxson != fa[x])       for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])         if (v != fa[x]) mge(rt[x], rt[v]);   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) {       int x = read(), y = read();       if (!x)         root = y;       else         fa[y] = x, add(x, y);     }     pre_dfs(root);     for (int i = 1; i <= n; ++i) ins(Rt, siz[i]);     solve_dfs(root);     for (int i = 1; i <= n; ++i) print(ans[i]), puts(  );     return 0;   }   