You are given a tree of size $$$n$$$ rooted at node $$$1$$$. Each node $$$u$$$ has a value $$$val_u$$$.
Let $$$dis(u, v)$$$ be the sum of the values of the nodes in the simple path from $$$u$$$ to $$$v$$$. Also, let $$$d_u = dis(1, u)$$$. Two nodes $$$u$$$ and $$$v$$$ are considered similar if $$$(d_u + d_v)^2 = (d_u - d_v)^2 = dis(u, v)$$$.
Your goal is to find the number of pairs $$$(u,v)$$$, $$$1 \le u \lt v \le n$$$, such that nodes $$$u$$$ and $$$v$$$ are similar.
The first line of the input contains one integer, $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$.
The second line of the input contains $$$n$$$ integers $$$val_1, val_2, ..., val_n$$$ $$$(-10^9 \le val_i \le 10^9)$$$, where $$$val_i$$$ is the value of the $$$i$$$-$$$th$$$ node.
The third line of the input contains $$$n-1$$$ integers $$$p_2, p_3, ..., p_n$$$ , where $$$p_i$$$ is the parent of the $$$i$$$-$$$th$$$ node.
It is guaranteed that the input forms a valid tree.
Output exactly one integer, the number of pairs $$$(u,v)$$$ $$$(1 \le u \lt v \le n)$$$ such that nodes $$$u$$$ and $$$v$$$ are similar.
60 1 0 -1 0 01 2 2 4 1
9
100 -7 0 5 7 0 -1 0 7 09 2 1 8 2 5 1 8 3
10
For the first example:
The pairs are $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(1, 6)$$$, $$$(2, 6)$$$, $$$(3, 6)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$.
Take as an example the pair $$$(4, 6)$$$. Because $$$d_4 = 0$$$, $$$d_6 = 0$$$, and $$$dis(4, 6) = 0$$$, then $$$(d_4 + d_6)^2 = (d_4 - d_6)^2 = dis(4, 6) = 0$$$. Therefore, the nodes $$$4$$$ and $$$6$$$ are similar.