创建于 2026/07/16 · 更新于 2026/07/16
You are given a rooted tree with vertices, rooted at vertex . Vertex has a non-negative integer capacity .
You may choose a set of vertices (any subset of the vertices). The set is called valid if, for every vertex of the tree, the number of chosen vertices lying in the subtree of is at most :
where is the subtree rooted at (including itself).
You must answer queries. Each query gives two integers and . For this query, you may only choose vertices whose index lies in , i.e. ; however, the capacity constraint above still applies to every vertex of the tree. Report the maximum possible size of a valid set.
Note. The capacity constraint holds for all vertices, not only for the vertices with index in . In particular, a vertex (such as a common ancestor or the root) still limits how many chosen vertices its subtree may contain.
The first line contains two integers and .
The second line contains integers .
The third line contains integers , where is the parent of vertex .
Each of the next lines contains two integers and .
, , , and where the parents describe a tree rooted at vertex . For each query, .
For each query, output a single line containing the maximum size of a valid chosen set.
8 4
4 1 2 1 1 1 1 1
1 1 2 2 3 3 1
1 8
4 8
4 5
6 7
4
4
1
2
4 2
2 0 1 2
1 1 2
1 4
4 4
2
0