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 vi. The root of each tree is vertex 1.Given K key numbers x1,…,xk,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 N,K(2≤K≤N≤105).
The second line has K unique positive integers x1,…,xk(xi≤N).
The third line has N positive integers ai(ai≤109)represents the weight of vertices in A.
The fourth line has N−1 positive integers {pai},indicating that the number of the father of vertices i+1 in tree A is pai.
The fifth line has n positive integers bi(bi≤109)represents the weight of vertices in B.
The sixth line has N−1 positive integers {pbi},indicating that the number of the father of vertices i+1 in tree B is pbi.
One integer indicating the answer.
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.