func_code_string: #include <bits/stdc++.h>   using namespace std;   const int maxn = 1000000;   int centroid[maxn];   int parent[maxn];   int subtree[maxn];   int heaviest[maxn];   vector<int> graph[maxn];   void dfs(int u, int p = 0) {     parent[u] = p;     subtree[u] = 1;     heaviest[u] = 0;     int vert = 0;     for (auto v : graph[u]) {       if (v == p) continue;       dfs(v, u);       subtree[u] += subtree[v];       if (subtree[v] > heaviest[u]) {         heaviest[u] = subtree[v];         vert = v;       }     }     if ((int)graph[u].size() == 0 or (2 * heaviest[u] <= subtree[u]))       centroid[u] = u;     else {       int curr = centroid[vert];       centroid[u] = curr;       while (curr != u) {         int P = parent[curr];         int rem = subtree[u] - subtree[P];         if (2 * heaviest[P] > subtree[u]) break;         if (2 * rem <= subtree[u]) centroid[u] = P;         curr = parent[curr];       }     }   }   int main() {     int n;     scanf( %d , &n);     int q;     scanf( %d , &q);     for (int i = 2; i <= n; i++) {       int p;       scanf( %d , &p);       graph[p].push_back(i);     }     dfs(1);     while (q--) {       int v;       scanf( %d , &v);       printf( %d n , centroid[v]);     }     return 0;   }   