CF1263F.Economic Difficulties

普及/提高-

通过率:0%

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 11 ). 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 nn nodes, which do not spread electricity further.

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

Also, the palace has nn 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 66 nodes (the top tree) and the reserve grid contains 44 nodes (the lower tree). There are 33 devices with numbers colored in blue.It is guaranteed that the whole grid (two grids and nn 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 11 to nn 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 11 , that visits leaves in order of connection to devices 1,2,,n1, 2, \dots, n (firstly, the node, that is connected to the device 11 , then the node, that is connected to the device 22 , 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.

输入格式

The first line contains an integer nn ( 1n10001 \le n \le 1000 ) — the number of devices in the palace.

The next line contains an integer aa ( 1+na1000+n1 + n \le a \le 1000 + n ) — the amount of nodes in the main grid.

Next line contains a1a - 1 integers pip_i ( 1pia1 \le p_i \le a ). Each integer pip_i means that the main grid contains a wire from pip_i -th node to (i+1)(i + 1) -th.

The next line contains nn integers xix_i ( 1xia1 \le x_i \le a ) — the number of a node in the main grid that is connected to the ii -th device.

The next line contains an integer bb ( 1+nb1000+n1 + n \le b \le 1000 + n ) — the amount of nodes in the reserve grid.

Next line contains b1b - 1 integers qiq_i ( 1qib1 \le q_i \le b ). Each integer qiq_i means that the reserve grid contains a wire from qiq_i -th node to (i+1)(i + 1) -th.

The next line contains nn integers yiy_i ( 1yib1 \le y_i \le b ) — the number of a node in the reserve grid that is connected to the ii -th device.

It is guaranteed that each grid is a tree, which has exactly nn 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 11 , that visits leaves in order of connection to devices.

输出格式

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:

首页