func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, sz[100005], msz[100005], xsz[100005], lsz[100005], son[100005],       ans[100005];   vector<int> G[100005];   void dfs(int u) {     sz[u] = 1;     lsz[u] = n;     son[u] = -1;     for (int v : G[u]) {       dfs(v), sz[u] += sz[v];       if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;       if (sz[v] > msz[u])         xsz[u] = msz[u], msz[u] = sz[v];       else         xsz[u] = max(xsz[u], sz[v]);       lsz[u] = min(lsz[u], sz[v]);     }     if (n - sz[u] > msz[u])       xsz[u] = msz[u], msz[u] = n - sz[u];     else       xsz[u] = max(xsz[u], n - sz[u]);     if (n - sz[u]) lsz[u] = min(lsz[u], n - sz[u]);   }   multiset<int> st, st2;   void ser1(int u) {     st.insert(sz[u]);     for (int v : G[u]) ser1(v);   }   void dfs1(int u) {     for (int v : G[u])       if (v != son[u]) {         dfs1(v);         st.clear();       }     if (son[u] != -1) dfs1(son[u]);     if (n - sz[u] != msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }     for (int v : G[u])       if (v != son[u]) ser1(v);     st.insert(sz[u]);   }   void ser2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u]) ser2(v);   }   void dfs2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u])       if (v != son[u]) {         st2.insert(sz[u]);         dfs2(v);         ser1(v);         st2.erase(st2.find(sz[u]));       }     if (son[u] != -1) {       int v = son[u];       st2.insert(sz[u]);       dfs2(v);       st2.erase(st2.find(sz[u]));     }     for (int v : G[u])       if (v != son[u]) ser2(v);     if (n - sz[u] == msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid),                                 it2 = st2.lower_bound(msz[u] - mid + sz[u]);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else if (it2 != st2.end() && (*it2) <= mid - lsz[u] + sz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }   }   int main() {     scanf( %d , &n);     for (int i = (1), LIM = (n); i <= LIM; i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);     }     dfs(0);     dfs1(G[0][0]);     dfs2(G[0][0]);     for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i]);   }   