Codeforces Round #603 (Div. 2) F. Economic Difficulties dp

F. Economic Difficulties

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个叶子,每个叶子都连着一个电机。

让你删除最多的边,使得每个电机都至少能够存在一条路径到某棵树的根。

note很清楚

题解

视频题解 https://www.bilibili.com/video/av77514280/

对于每棵树,维护l[i][j]表示我删掉这个子树的所有边之后,[i,j]这个范围的电机不保证能够全部连上我的根。

用一个dp[i]表示[1,i]区间内,全都能连上根最多能删除多少条边,那么转移就是dp[i]=max(dp[i],dp[j-1]+max(l[j][i]));这样转移。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
vector<int>G[2][maxn];
int dp[maxn],val[2][maxn][maxn],l[2][maxn],r[2][maxn],sz[2][maxn];
int n,a;
void dfs(int _,int x){
    if(x!=1)sz[_][x]=1;
    for(int i=0;i<G[_][x].size();i++){
        int v = G[_][x][i];
        dfs(_,v);
        l[_][x]=min(l[_][x],l[_][v]);
        r[_][x]=max(r[_][x],r[_][v]);
        sz[_][x]+=sz[_][v];
    }
    val[_][l[_][x]][r[_][x]]=max(val[_][l[_][x]][r[_][x]],sz[_][x]);
}
int main(){
    cin>>n;
    for(int _=0;_<2;_++){
        cin>>a;
        for(int i=1;i<=a;i++){
            l[_][i]=a+1;
            r[_][i]=0;
        }
        for(int i=2;i<=a;i++){
            int x;cin>>x;
            G[_][x].push_back(i);
        }
        for(int i=1;i<=n;i++){
            int x;cin>>x;
            l[_][x]=r[_][x]=i;
        }
        dfs(_,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]));
        }
    }
    cout<<dp[n]<<endl;
}

原文地址:https://www.cnblogs.com/qscqesze/p/11961873.html

时间: 2024-08-29 05:43:13

Codeforces Round #603 (Div. 2) F. Economic Difficulties dp的相关文章

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

题目链接:Codeforces Round #369 (Div. 2) C. Coloring Trees 题意: 有n个树,每个树有一个颜色,如果颜色值为0,表示没有颜色,一共有m个颜色,第j种颜色涂第i棵树需要花费pij,颜色一样且相邻的分为一组 现在要将所有颜色为0的树涂上颜色,使得这些树恰好可以分为k组,问最小的花费 题解: 考虑dp[i][j][k],表示考虑第i棵树涂第j种颜色,当前分为k组的最小花费,然后状态转移看代码,注意的是dp的初始状态 1 #include<bits/std

Codeforces Round #455 (Div. 2) C. Python Indentation dp递推

Codeforces Round #455 (Div. 2) C. Python Indentation 题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句.按 python 缩进的方式组合成合法的程序,问有多少种可能方案. tags: dp dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移: 1] 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j] = dp[i][j]. 2]如果

Codeforces Round #392 (Div. 2) F. Geometrical Progression

原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output For given n, l and r find the number of distinct geometrical pro

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是 1. \(p_i \leq inc_j \leq s_i\) 2. \(|b_i - pref_j| \leq inc_j-p_i\) ,问每个人分别能买多少道菜 题解 转化一下公式 \(p_i \leq inc_j \leq s_i\) 下面两个满