func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, m;   int A[600006];   vector<int> G[600006];   int dfn[600006], bc[600006], clo;   int g[600006][20], dep[600006];   void dfs(int u) {     dfn[u] = ++clo, bc[clo] = u;     if (u == 1) dep[u] = 1;     for (int v : G[u]) {       g[v][0] = u;       dep[v] = dep[u] + 1;       for (int k = (1), kend = (18); k <= kend; ++k)         if (g[g[v][k - 1]][k - 1])           g[v][k] = g[g[v][k - 1]][k - 1];         else           break;       dfs(v);     }   }   int lca(int u, int v) {     if (dep[u] < dep[v]) swap(u, v);     if (dep[u] != dep[v])       for (int k = (18), kend = (0); k >= kend; --k)         if (dep[g[u][k]] >= dep[v]) u = g[u][k];     if (u == v) return u;     for (int k = (18), kend = (0); k >= kend; --k)       if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k];     return g[u][0];   }   int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }   set<int> S;   void solve() {     cin >> n;     ++n;     for (int i = (2), iend = (n); i <= iend; ++i) {       int p;       scanf( %d , &p);       G[p].push_back(i);     }     dfs(1);     int s = 1, t = 1, lt = 0;     auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); };     S.insert(1);     for (int i = (2), iend = (n); i <= iend; ++i) {       int is = dis(i, s), it = dis(i, t), flg = 0;       if (is >= lt && is >= it)         t = i, lt = is, flg = 1;       else if (it >= lt && it >= is)         s = i, lt = it, flg = 1;       if (!flg) {         printf( %d n , (int)S.size());         continue;       }       auto tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.end()) {         if (wkr(bc[*tr]) < lt)           ++tr;         else           break;       }       if (tr == S.end()) {         auto sr = S.begin();         while (sr != st) {           if (wkr(bc[*sr]) < lt)             ++sr;           else             break;         }         S.erase(S.begin(), sr);       }       S.erase(st, tr);       tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.begin()) {         --tr;         if (wkr(bc[*tr]) >= lt) {           ++tr;           break;         }       }       if (tr == S.begin()) {         auto sr = S.end();         while (sr != st) {           --sr;           if (wkr(bc[*sr]) >= lt) {             ++sr;             break;           }         }         S.erase(sr, S.end());       }       S.erase(tr, st);       S.insert(dfn[i]);       printf( %d n , (int)S.size());     }   }   signed main() { solve(); }   