8521: Ancestor

内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

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.

分类标签