func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const int D = 30;   struct node {     int a, b;   };   vector<int> road[N], h[N];   vector<node> q[N];   int n, m, t, fa[N][D + 1], w[N][D + 1], head[N], sum[N], ans[N], g[N];   int cnt, tot, dep[N], id[N], size[N], c[N];   int lowbit(int x) { return x & -x; }   void update(int x) {     for (int i = x; i <= n; i += lowbit(i)) {       c[i] += 1;     }   }   int check(int x) {     int tmp = 0;     for (int i = x; i >= 1; i -= lowbit(i)) {       tmp += c[i];     }     return tmp;   }   struct edge {     int to, nxt;   } e[N * 2];   void add(int u, int v) {     cnt++;     e[cnt].nxt = head[u];     e[cnt].to = v;     head[u] = cnt;   }   void dfs1(int u, int deep) {     dep[u] = deep;     id[u] = ++tot;     size[u] = 1;     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa[u][0]) continue;       dfs1(v, deep + 1);       size[u] += size[v];     }   }   void dfs2(int u) {     g[u] = u;     for (int i = 0; i < h[u].size(); i++) {       if (dep[h[u][i]] < dep[g[u]]) g[u] = h[u][i];     }     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa[u][0]) continue;       dfs2(v);       if (dep[g[v]] < dep[g[u]]) g[u] = g[v];     }     w[u][0] = g[u];   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = D; i >= 0; i--) {       if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];     }     if (x == y) return x;     for (int i = D; i >= 0; i--) {       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     }     return fa[x][0];   }   node work(int x, int LCA) {     if (x == LCA) return (node){x, 0};     int tmp = 0;     for (int i = D; i >= 0; i--)       if (dep[w[x][i]] > dep[LCA]) x = w[x][i], tmp += (1 << i);     if (w[x][0] == x) return (node){x, -1};     return (node){x, tmp};   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       scanf( %d , &fa[i][0]);       add(fa[i][0], i);       add(i, fa[i][0]);     }     dfs1(1, 1);     for (int i = 1; i <= D; i++)       for (int j = 1; j <= n; j++) {         fa[j][i] = fa[fa[j][i - 1]][i - 1];       }     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       int c = lca(x, y);       if (id[x] > id[y]) swap(x, y);       road[id[x]].push_back(id[y]);       if (dep[c] < dep[x]) h[x].push_back(c);       if (dep[c] < dep[y]) h[y].push_back(c);     }     dfs2(1);     for (int i = 1; i <= n; i++) {     }     for (int i = 1; i <= D; i++)       for (int j = 1; j <= n; j++) {         w[j][i] = w[w[j][i - 1]][i - 1];       }     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       int x, y;       scanf( %d%d , &x, &y);       int c = lca(x, y);       node A = work(x, c);       node B = work(y, c);       if (A.b == -1 || B.b == -1) {         ans[i] = -1;         continue;       }       if (x == c || y == c) {         ans[i] = A.b + B.b + 1;         continue;       }       ans[i] = A.b + B.b + 2;       x = A.a;       y = B.a;       if (id[x] > id[y]) swap(x, y);       q[id[x] - 1].push_back((node){y, -i});       q[id[x] + size[x] - 1].push_back((node){y, i});     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < road[i].size(); j++) update(road[i][j]);       for (int j = 0; j < q[i].size(); j++) {         int num = (q[i][j].b > 0 ? 1 : -1);         int x = q[i][j].a;         int y = q[i][j].b * num;         sum[y] += num * (check(id[x] + size[x] - 1) - check(id[x] - 1));       }     }     for (int i = 1; i <= t; i++) {       printf( %d n , ans[i] - (sum[i] > 0));     }     return 0;   }   