func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 100100;   const long long MOD = 1000000007;   const double EBS = 1e-7;   int dX[] = {0, 0, 1, -1};   int dY[] = {1, -1, 0, 0};   long long GCD(long long num, long long ones) {     return !ones ? num : GCD(ones, num % ones);   }   const int L = 17;   vector<int> adj[N];   vector<int> Root;   vector<int> level[N];   int depth[N], ID[N], rID[N], dp[L + 1][N], currID = 1, n, m, k, WantedNode;   void dfs(int cur, int par) {     dp[0][cur] = par;     ID[cur] = currID++;     rID[ID[cur]] = cur;     level[depth[cur]].push_back(ID[cur]);     for (auto x : adj[cur])       if (x != par) depth[x] = depth[cur] + 1, dfs(x, cur);   }   inline int getKthAncestor(int u, int k) {     int d = depth[u] - k;     for (int j = L; j >= 0; --j) {       int nu = dp[j][u];       if (!nu) continue;       if (depth[nu] == d)         return nu;       else if (depth[nu] > d)         u = nu;     }     return 0;   }   inline int BSL(int d) {     int l = 0, r = (level[d].size()) - 1, mid;     while (l < r) {       mid = l + (r - l) / 2;       int node = getKthAncestor(rID[level[d][mid]], k);       node = ID[node];       if (node >= WantedNode)         r = mid;       else         l = mid + 1;     }     return r;   }   inline int BSR(int d) {     int l = 0, r = (level[d].size()) - 1, mid;     while (l < r) {       mid = l + (r - l + 1) / 2;       int node = getKthAncestor(rID[level[d][mid]], k);       node = ID[node];       if (node <= WantedNode)         l = mid;       else         r = mid - 1;     }     return l;   }   int main() {     scanf( %d , &n);     int p, v;     for (int i = 0; i < n; ++i) {       scanf( %d , &p);       if (p)         adj[p].push_back(i + 1), adj[i + 1].push_back(p);       else         Root.push_back(i + 1);     }     for (auto x : Root) dfs(x, 0);     for (int j = 1; j <= L; ++j)       for (int i = 0; i < N; ++i)         dp[j][i] = (dp[j - 1][i] == -1 ? -1 : dp[j - 1][dp[j - 1][i]]);     scanf( %d , &m);     while (m--) {       scanf( %d , &v);       scanf( %d , &k);       WantedNode = getKthAncestor(v, k);       if (WantedNode) {         WantedNode = ID[WantedNode];         printf( %d  , BSR(depth[v]) - BSL(depth[v]));       } else         printf( 0  );     }     return 0;   }   