#P6623. [省选联考 2020 A 卷] 树

[省选联考 2020 A 卷] 树

Description

Given a rooted tree TT with nn nodes. The nodes are numbered starting from 11. The root is node 11. Each node has a positive integer weight viv_i.

Let the node numbers of all nodes in the subtree of node xx (including xx itself) be c1,c2,,ckc_1,c_2,\dots,c_k. Define the value of xx as:

$val(x)=(v_{c_1}+d(c_1,x)) \oplus (v_{c_2}+d(c_2,x)) \oplus \cdots \oplus (v_{c_k}+d(c_k, x))$

Here, d(x,y)d(x,y) denotes the number of edges on the unique simple path between nodes xx and yy in the tree, and d(x,x)=0d(x, x) = 0. \oplus denotes the XOR operation.

Please compute i=1nval(i)\sum\limits_{i=1}^n val(i).

Input Format

The first line contains a positive integer nn, representing the size of the tree.

The second line contains nn positive integers, representing viv_i.

The next line contains n1n-1 positive integers, in order representing the parent index pip_i of nodes 22 to nn.

Output Format

Only one line containing one integer, representing the answer.

5
5 4 1 2 3
1 1 2 2
12

Hint

[Sample Explanation 1]

$val(1)=(5+0)\oplus(4+1)\oplus(1+1)\oplus(2+2)\oplus(3+2)=3$.

val(2)=(4+0)(2+1)(3+1)=3val(2)=(4+0)\oplus(2+1)\oplus(3+1) = 3.

val(3)=(1+0)=1val(3)=(1+0)=1.

val(4)=(2+0)=2val(4)=(2+0)=2.

val(5)=(3+0)=3val(5)=(3+0)=3.

The sum is 1212.

[Constraints]

For 10%10\% of the testdata: 1n25011\leq n\leq 2501.

For 40%40\% of the testdata: 1n1525011\leq n\leq 152501.

Another 20%20\% of the testdata: all pi=i1p_i=i-1 (2in2\leq i\leq n).

Another 20%20\% of the testdata: all vi=1v_i=1 (1in1\leq i\leq n).

For 100%100\% of the testdata: 1n,vi5250101\leq n,v_i \leq 525010, 1pin1\leq p_i\leq n.

Translated by ChatGPT 5