CF1263F.Economic Difficulties
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
贝尔兰宫殿的电网由两个子网组成:主电网和备用电网。宫殿中的电线由昂贵材料制成,因此出售其中一部分将是一个不错的主意!
每个子网(主电网和备用电网)均有一个头节点(编号为 1)。其余所有节点均由该头节点供电。从头节点出发,到达任意其他节点的路径都是唯一的。此外,两个子网均恰好含有 n 个不再向下传输电能的节点。
换言之,每个子网均是一棵以编号为 1 的节点为根、含 n 片叶子的有向根树。两棵树各自独立编号,且一棵树中的节点与另一棵树中的节点互不相连。
此外,宫殿内共有 n 个电气设备。每个设备均连接主电网的一个节点和备用电网的一个节点。设备仅连接那些不再向下传输电能的节点(即树的叶子节点),且每个子网的每片叶子恰好连接一个设备。
在本示例中,主电网包含 6 个节点(上方的树),备用电网包含 4 个节点(下方的树)。共有 3 个设备(编号以蓝色标出)。
题目保证整个电网(两个子网及 n 个设备)均可按上述方式(如上图所示)呈现:
- 主电网为上方的树,其导线方向为“自上而下”;
- 备用电网为下方的树,其导线方向为“自下而上”;
- 设备呈水平排列于两棵树之间,从左至右依次编号为 1 至 n;
- 节点之间的导线互不相交。
形式化地,对每棵树均存在一种从编号为 1 的节点出发的深度优先遍历,使得其访问叶子节点的顺序恰好为设备连接顺序 1,2,…,n(即先访问连接设备 1 的叶子节点,再访问连接设备 2 的叶子节点,依此类推)。
一位商人希望出售(即移除)尽可能多的导线,同时确保每个设备至少能从一个子网(主电网或备用电网)获得电力。换言之,对每个设备,必须存在一条通往头节点的路径(在主电网中或在备用电网中),且该路径仅包含同一子网内的节点。
输入格式
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.
第一行包含一个整数 n(1≤n≤1000)——宫殿中设备的数量。
第二行包含一个整数 a(1+n≤a≤1000+n)——主电网中的节点数量。
第三行包含 a−1 个整数 pi(1≤pi≤a)。每个整数 pi 表示主电网中存在一条从第 pi 个节点到第 (i+1) 个节点的导线。
第四行包含 n 个整数 xi(1≤xi≤a)——第 i 个设备所连接的主电网中节点的编号。
第五行包含一个整数 b(1+n≤b≤1000+n)——备用电网中的节点数量。
第六行包含 b−1 个整数 qi(1≤qi≤b)。每个整数 qi 表示备用电网中存在一条从第 qi 个节点到第 (i+1) 个节点的导线。
第七行包含 n 个整数 yi(1≤yi≤b)——第 i 个设备所连接的备用电网中节点的编号。
保证每个电网均构成一棵树,且恰好具有 n 个叶节点,每个叶节点恰好连接一个设备。此外,还保证对每棵树均存在一种从节点 1 出发的深度优先搜索遍历方式,使得叶节点被访问的顺序恰好与其所连接设备的编号顺序一致。
输出格式
Print a single integer — the maximal amount of wires that can be cut so that each device is powered.
输出一个整数——在保证每个设备都有电的情况下,最多可以切断的导线数量。
输入输出样例
输入#1
3 6 4 1 1 4 2 6 5 3 4 1 1 1 3 4 2
输出#1
5
输入#2
4 6 4 4 1 1 1 3 2 6 5 6 6 6 1 1 1 5 4 3 2
输出#2
6
输入#3
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
输出#3
17
说明/提示
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:

对于第一个样例,下图展示了一种可能的解法(可移除的导线用红色标记):

第二个和第三个样例如下所示:

输入解题思路,AI测评打分。不知道怎么写?