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;
}

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

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

    F. Economic Difficulties 题目链接: https://codeforces.com/contest/1263/problem/F 题目大意: 两棵树,都有n个叶子节点,一棵树正 ...

  2. Codeforces Round #471 (Div. 2) F. Heaps(dp)

    题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...

  3. Codeforces Round #527 (Div. 3)F(DFS,DP)

    #include<bits/stdc++.h>using namespace std;const int N=200005;int n,A[N];long long Mx,tot,S[N] ...

  4. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)

    题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

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

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

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

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

随机推荐

  1. LeetCode刷题191125

    博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 今天状态不好,划一下水. 算法: 给定一个整数数组 nums 和一个目标 ...

  2. 使用Apollo动态修改线上数据源

    前言 最近需要实现一个功能,动态刷新线上数据源环境,下面来使用Apollo配置中心和Spring提供的AbstractRoutingDataSource来实现. 具体实现 Apollo是携程开源的统一 ...

  3. java执行-cp报错 error: could not load JDBC driver

    首先查看对应的 jar 包是否存在,然后看一下 Server (获取数据库驱动类的名称 driverClassName)是否正确 例如: java -

  4. pymysql用法,Python连接MySQL数据库

    Pymysql模块是专门用来操作mysql数据库的模块,使用前需要安装,安装指令:pip install pymysql 操作流程: 第一步:import pymysql 第二步:获取数据库的连接 , ...

  5. Cisdem OCRWizard for Mac 使用教程

    Cisdem OCRWizard for Mac是一款macOS平台的的文件识别软件,可以对任何PDF.扫描文件或图像文件(包括名片的照片)进行OCR识别,支持超过40种语言,帮助我们提高效率.现在小 ...

  6. tensor的加减乘和矩阵乘法

    对应的代码如下 import torch """如下是tensor乘法与加减法,对应位相乘或相加减,可以一对多""" def add_and ...

  7. Java之System类

    System类概述 java.lang.System 类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static lon ...

  8. react 项目引入路由

    下载路由包 npm i react-router-dom -d 前台路由 登陆: /login /login.jsx App.js import React ,{Component} from 're ...

  9. libwebrtc & libmediasoupclient编译

    本文简单介绍在Ubuntu下libwebrtc的编译过程. 由于网速限制,实际编译过程是在远程vps上编译滴. 系统环境 Ubuntu 18.04系统的虚拟主机. root@vultr:~# pwd ...

  10. 朝花夕拾《精通CSS》一、HTML & CSS 的基础

    一.背景 翻出我4年前看的<精通CSS>一书,可惜当初没有整理读书笔记的习惯,最近又很少写前端,遂很多东西.知识点遗忘了,恰且现在 css 也有些变化和进步,遂一起打包整理,输出成几篇 b ...