Economic Difficulties CodeForces - 1263F(DP)

An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!
在这里插入图片描述
Each grid (main and reserve) has a head node (its number is 1). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly n nodes, which do not spread electricity further.

In other words, every grid is a rooted directed tree on n leaves with a root in the node, which number is 1. Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid.

Also, the palace has n electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree’s leaves). Each grid’s leaf is connected with exactly one device.

In this example the main grid contains 6 nodes (the top tree) and the reserve grid contains 4 nodes (the lower tree). There are 3 devices with numbers colored in blue.
It is guaranteed that the whole grid (two grids and n devices) can be shown in this way (like in the picture above):

main grid is a top tree, whose wires are directed ‘from the top to the down’,
reserve grid is a lower tree, whose wires are directed ‘from the down to the top’,
devices — horizontal row between two grids, which are numbered from 1 to n from the left to the right,
wires between nodes do not intersect.
Formally, for each tree exists a depth-first search from the node with number 1, that visits leaves in order of connection to devices 1,2,…,n (firstly, the node, that is connected to the device 1, then the node, that is connected to the device 2, etc.).

Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid.

Input
The first line contains an integer n (1≤n≤1000) — the number of devices in the palace.

The next line contains an integer a (1+n≤a≤1000+n) — the amount of nodes in the main grid.

Next line contains a−1 integers pi (1≤pi≤a). Each integer pi means that the main grid contains a wire from pi-th node to (i+1)-th.

The next line contains n integers xi (1≤xi≤a) — the number of a node in the main grid that is connected to the i-th device.

The next line contains an integer b (1+n≤b≤1000+n) — the amount of nodes in the reserve grid.

Next line contains b−1 integers qi (1≤qi≤b). Each integer qi means that the reserve grid contains a wire from qi-th node to (i+1)-th.

The next line contains n integers yi (1≤yi≤b) — the number of a node in the reserve grid that is connected to the i-th device.

It is guaranteed that each grid is a tree, which has exactly n leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node 1, that visits leaves in order of connection to devices.

Output
Print a single integer — the maximal amount of wires that can be cut so that each device is powered.

Examples
Input
3
6
4 1 1 4 2
6 5 3
4
1 1 1
3 4 2
Output
5
Input
4
6
4 4 1 1 1
3 2 6 5
6
6 6 1 1 1
5 4 3 2
Output
6
Input
5
14
1 1 11 2 14 14 13 7 12 2 5 6 1
9 8 3 10 4
16
1 1 9 9 2 5 10 1 14 3 7 11 6 12 2
8 16 13 4 15
Output
17
Note
For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red):

在这里插入图片描述
The second and the third examples can be seen below:
在这里插入图片描述

题意:
两棵树,每棵树的n个叶子都各连着一个机器。
要求删除最多的点,使得每个机器都有一条路走到1号节点(两棵树的根节点均为1号节点)。
思路:
n≤1000,意味着可以n方。
本题类似背包,连续选几个物品会有额外收益。
设置l ,r数组代表树上每个点最左(右)边连接的是哪个机器。
再设置val[0/1][x][y]代表选x~y个机器的时候最多删除多少个边。
这几个数组可以用dfs维护出来。
之后就可以n2转移了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 2005;

vector<int>G[2][maxn];
int l[2][maxn],r[2][maxn],siz[2][maxn],val[2][maxn][maxn];
int dp[maxn];

void dfs(int num,int u)
{
    if(u != 1)siz[num][u] = 1;
    for(int i = 0;i < G[num][u].size();i++)
    {
        int v = G[num][u][i];
        dfs(num,v);
        l[num][u] = min(l[num][u],l[num][v]);
        r[num][u] = max(r[num][u],r[num][v]);
        siz[num][u] += siz[num][v];
    }
    val[num][l[num][u]][r[num][u]] = max(val[num][l[num][u]][r[num][u]],siz[num][u]);
}

int main()
{
    int n;scanf("%d",&n);
    for(int num = 0;num <= 1;num++)
    {
        int a;scanf("%d",&a);
        for(int i = 1;i <= a;i++)
        {
            l[num][i] = a + 1;
            r[num][i] = 0;
        }
        for(int i = 2;i <= a;i++)
        {
            int x;scanf("%d",&x);
            G[num][x].push_back(i);
        }
        for(int i = 1;i <= n;i++)
        {
            int x;scanf("%d",&x);
            l[num][x] = i;
            r[num][x] = i;
        }
        dfs(num,1);
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = i;j <= n;j++)
        {
            dp[j] = max(dp[j],dp[i - 1] + max(val[0][i][j],val[1][i][j]));
        }
    }
    printf("%d\n",dp[n]);
    return 0;
}
发布了676 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104125141