Ancestor
题号:NC239569
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

NIO is playing a game about trees.

The game has two trees A, B each with N vertices. The vertices in each tree are numbered from 1 to N and the i-th vertex has the weight v_i. The root of each tree is vertex 1. Given K key numbers , find the number of solutions that remove exactly one number so that the weight of the lowest common ancestor of the vertices in A with the remaining key numbers is greater than the weight of the lowest common ancestor of the vertices in B with the remaining key numbers.

输入描述:

The first line has two positive integers .

The second line has K unique positive integers .

The third line has N positive integers represents the weight of vertices in A.

The fourth line has N - 1 positive integers , indicating that the number of the father of vertices in tree A is pa_i.

The fifth line has n positive integers represents the weight of vertices in B.

The sixth line has N - 1 positive integers , indicating that the number of the father of vertices in tree B is pb_i.

输出描述:

One integer indicating the answer.
示例1

输入

复制
5 3
5 4 3
6 6 3 4 6
1 2 2 4
7 4 5 7 7
1 1 3 2

输出

复制
1

说明

In first case, the key numbers are 5,4,3. 
Remove key number 5, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 3.
Remove key number 4, the lowest common ancestors of the vertices in A with the remaining key numbers is 2, in B is 1.
Remove key number 3, the lowest common ancestors of the vertices in A with the remaining key numbers is 4, in B is 1.
Only remove key number 5 satisfies the requirement.
示例2

输入

复制
10 3
10 9 8
8 9 9 2 7 9 0 0 7 4
1 1 2 4 3 4 2 4 7
7 7 2 3 4 5 6 1 5 3
1 1 3 1 2 4 7 3 5

输出

复制
2

备注:

The lowest common ancestor (LCA) (also called least common ancestor) of two nodes v and w in a tree or directed acyclic graph (DAG) T is the lowest (i.e. deepest) node that has both v and w as descendants, where we define each node to be a descendant of itself (so if v has a direct connection from w, w is the lowest common ancestor).(From Wiki.)