func_code_string: #include <bits/stdc++.h>   using namespace std;   const int oo = 1e9;   const int MOD = 1000000007;   const int N = 100010;   vector<int> g[N];   multiset<int> seg[4 * N], stk;   int n, u, v, sz[N], st[N], ed[N], bk[N], cnt = 1, res[N];   void dfs(int u, int p) {     sz[u] = 1, bk[cnt] = u, st[u] = cnt++;     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       dfs(v, u);       sz[u] += sz[v];     }     ed[u] = cnt - 1;   }   void update(int n, int s, int e, int at, int val, int type) {     if (s > at || e < at) return;     if (type == 0) {       seg[n].insert(val);     } else {       seg[n].erase(seg[n].find(val));     }     if (s == e) return;     update(2 * n, s, (s + e) / 2, at, val, type);     update(2 * n + 1, (s + e) / 2 + 1, e, at, val, type);   }   inline int check(multiset<int> &mst, int mn, int mx, int OFFSET) {     int ans = 1e9;     int S = (mx - mn) / 2 + OFFSET;     multiset<int>::iterator it = mst.lower_bound(S);     for (int f = 0; f < 2; ++f) {       if (it != mst.end() && (*it != n))         ans = min(ans, max(mx - (*it - OFFSET), mn + (*it - OFFSET)));       if (it == mst.begin()) break;       it--;     }     return ans;   }   int l, r, mn, mx;   int get(int n, int s, int e) {     if (s > r || e < l) return oo;     if (s >= l && e <= r) return check(seg[n], mn, mx, 0);     return min(get(2 * n, s, (s + e) / 2), get(2 * n + 1, (s + e) / 2 + 1, e));   }   int get(int _l, int _r, int _mn, int _mx) {     l = _l;     r = _r;     mn = _mn;     mx = _mx;     if (l > r) return oo;     return get(1, 1, n);   }   void solve(int u, int p) {     pair<int, int> mx(-1, -1), mn(1e9, -1);     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       mx = max(mx, make_pair(sz[v], v));       mn = min(mn, make_pair(sz[v], v));     }     if (p != -1) {       mx = max(mx, make_pair(n - sz[u], p));       mn = min(mn, make_pair(n - sz[u], p));     }     update(1, 1, n, st[u], sz[u], 1);     int ans = max(0, mx.first);     if (mx.first != mn.first) {       if (mx.second == p) {         ans = min(ans, get(1, st[u] - 1, mn.first, mx.first));         ans = min(ans, get(ed[u] + 1, n, mn.first, mx.first));         ans = min(ans, check(stk, mn.first, mx.first, sz[u]));       } else {         ans = min(ans, get(st[mx.second], ed[mx.second], mn.first, mx.first));       }       for (int i = 0; i < g[u].size(); ++i) {         int v = g[u][i];         if (v == mx.second || v == mn.second) continue;         ans = max(ans, sz[v]);       }       if (p != -1 && mx.second != p && mn.second != p) {         ans = max(ans, n - sz[u]);       }     }     res[u] = ans;     stk.insert(sz[u]);     for (int i = 0; i < g[u].size(); ++i) {       solve(g[u][i], u);     }     stk.erase(stk.find(sz[u]));     update(1, 1, n, st[u], sz[u], 0);   }   int root = -1;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &u, &v);       if (!u) {         root = v;         continue;       }       g[u].push_back(v);     }     dfs(root, -1);     for (int i = 1; i <= n; ++i) update(1, 1, n, st[i], sz[i], 0);     solve(root, -1);     for (int i = 1; i <= n; ++i) {       printf( %d n , res[i]);     }     return 0;   }   