func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 * 3 + 10;   int n, k, m;   vector<int> graph[2][MAXN];   int p[2][MAXN];   int sum;   void read_input() {     scanf( %d , &n);     scanf( %d , &k);     int v;     for (int i = 0; i < k - 1; i++) {       sum++;       scanf( %d , &v);       graph[0][n + i + 2].push_back(v + n);       graph[0][v + n].push_back(n + i + 2);       p[0][n + i + 2] = v + n;     }     for (int i = 0; i < n; i++) {       scanf( %d , &v);       graph[0][i + 1].push_back(v + n);       graph[0][v + n].push_back(i + 1);       p[0][i + 1] = n + v;     }     scanf( %d , &m);     for (int i = 0; i < m - 1; i++) {       sum++;       scanf( %d , &v);       graph[1][n + i + 2].push_back(v + n);       graph[1][v + n].push_back(n + i + 2);       p[1][n + i + 2] = v + n;     }     for (int i = 0; i < n; i++) {       scanf( %d , &v);       graph[1][i + 1].push_back(v + n);       graph[1][v + n].push_back(i + 1);       p[1][i + 1] = n + v;     }   }   int timer;   int in[2][MAXN], out[2][MAXN];   int depth[2][MAXN];   void dfs(int v, int p, int r, int dt = 0) {     in[r][v] = timer++;     depth[r][v] = dt;     for (auto x : graph[r][v]) {       if (in[r][x] == 0 && x != p) {         dfs(x, v, r, dt + 1);       }     }     out[r][v] = timer++;   }   int lca[2][MAXN][MAXN];   const int INF = 1e9;   int _abs(int f) {     if (f < 0) return -f;     return f;   }   int T[MAXN][MAXN];   int rec(int l, int r) {     int temp = max(l, r) + 1;     if (temp > n) return 0;     if (T[l][r]) return T[l][r];     int ans = INF;     int add = 0;     if (l != 0) {       int LCA = lca[0][l][temp];       add = _abs(depth[0][LCA] - depth[0][temp]) - 1;       ans = min(ans, rec(temp, r) + add);     } else {       ans = rec(temp, r) + depth[0][temp] - 1;     }     if (r != 0) {       int LCA = lca[1][r][temp];       add = _abs(depth[1][LCA] - depth[1][temp]) - 1;       ans = min(ans, rec(l, temp) + add);     } else {       ans = min(ans, rec(l, temp) + depth[1][temp] - 1);     }     return T[l][r] = ans;   }   void solve() {     timer = 1;     dfs(n + 1, n + 1, 0);     for (int i = 0; i < n; i++) {       int temp = i + 1;       int LCA = temp;       for (int j = i + 1; j < n; j++) {         int tr = j + 1;         while (in[0][tr] > out[0][LCA] || out[0][tr] < in[0][LCA])           LCA = p[0][LCA];         lca[0][temp][tr] = lca[0][tr][temp] = LCA;       }     }     timer = 1;     dfs(n + 1, n + 1, 1);     for (int i = 0; i < n; i++) {       int temp = i + 1;       int LCA = temp;       for (int j = i + 1; j < n; j++) {         int tr = j + 1;         while (in[1][tr] > out[1][LCA] || out[1][tr] < in[1][LCA])           LCA = p[1][LCA];         lca[1][temp][tr] = lca[1][tr][temp] = LCA;       }     }     printf( %d n , sum - rec(0, 0));   }   int main() {     read_input();     solve();     return 0;   }   