func_code_string: #include <bits/stdc++.h>   using namespace std;   int n;   struct hh {     int t, nxt;   } edge[1000020 << 1];   int head[1000020], ecnt;   void make_edge(int f, int t) {     edge[++ecnt] = (hh){t, head[f]};     head[f] = ecnt;     edge[++ecnt] = (hh){f, head[t]};     head[t] = ecnt;   }   int dep[1000020], size[1000020], son[1000020], fa[1000020];   void dfs1(int x, int fa) {     dep[x] = dep[fa] + 1;     size[x] = 1;     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa) {         dfs1(edge[i].t, x);         size[x] += size[edge[i].t];         if (size[edge[i].t] > size[son[x]]) son[x] = edge[i].t;       }   }   map<string, int> S[1000020];   int sum[1000020];   string a[1000020];   bool s[1000020];   void add(string s, int d, int t) {     if (!S[d][s]) ++sum[d];     S[d][s] += t;     if (!S[d][s]) --sum[d];   }   void add(int x, int fa, int t) {     add(a[x], dep[x], t);     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa && !s[edge[i].t]) add(edge[i].t, x, t);   }   vector<pair<int, int> > q[1000020];   int ans[1000020];   void dfs2(int x, int fa, bool keep) {     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa && edge[i].t != son[x]) dfs2(edge[i].t, x, 0);     s[son[x]] = 1;     if (son[x]) dfs2(son[x], x, 1);     add(x, fa, 1);     for (int i = 0; i < (int)q[x].size(); i++)       ans[q[x][i].first] = sum[q[x][i].second];     s[son[x]] = 0;     if (!keep) add(x, fa, -1);   }   int main() {     ios::sync_with_stdio(false);     int i, x, y, m;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i] >> fa[i];       if (fa[i]) make_edge(fa[i], i);     }     for (i = 1; i <= n; i++)       if (!fa[i]) dfs1(i, 0);     cin >> m;     for (i = 1; i <= m; i++) {       cin >> x >> y;       q[x].push_back(make_pair(i, dep[x] + y));     }     for (i = 1; i <= n; i++)       if (!fa[i]) dfs2(i, 0, 0);     for (i = 1; i <= m; i++) printf( %d n , ans[i]);   }   