P17814 [ICPC 2019 Nanchang R] Tree

Description

Consider a rooted tree consisting of $n$ vertices where the first vertex is the root. The $i$-th vertex is endowed with the value $v_i$. Now you are given a positive integer $k$. An ordered pair of vertices $(x,y)$, indicating the $x$-th and the $y$-th vertices in the tree, is valid if it satisfies the following conditions: - $x\neq y$; - The $x$-th vertex is not an ancestor of the $y$-th one; - The $y$-th vertex is not an ancestor of the $x$-th one; - The length of the shortest path connection these two vertices is up to $k$; - Take the value of the lowest common ancestor of these two vertices, denoted by $v_z$. Then $v_x+v_y=2v_z$. Can you calculate the total number of valid pairs of vertices in this tree?

Input Format

The first line contains two integers $n$ and $k~(1 \le n, k \le 10^5)$ which is described as above. The second line contains $n$ integers, the $i$-th of which is the value of the $i$-th vertex $v_i$ ($0 \le v_i \le n$). The third line contains $n-1$ integers, the $i$-th of which represents the index of the parent vertex of the $(i+1)$-th vertex. We guarantee that the given tree is indeed a tree.

Output Format

Output the number of valid pairs of vertices in a line. Two pairs $(x_1,y_1)$ and $(x_2,y_2)$ are regarded as the same if $x_1=x_2$ and $y_1=y_2$.