#P7310. [COCI 2018/2019 #2] Deblo

[COCI 2018/2019 #2] Deblo

Description

Given a tree with nn nodes, where each node has a weight. The value of a path is defined as the XOR of the weights of all nodes on that path.

Your task is to compute the sum of the values of all paths.

Input Format

The first line contains a positive integer NN, representing the number of nodes in the tree.

The second line contains NN integers viv_i separated by spaces, where the ii-th integer represents the weight of node ii.

The next N1N - 1 lines each contain two integers aj,bja_j, b_j, indicating that there is an edge between aja_j and bjb_j.

Output Format

Output the sum of the values of all paths.

3
1 2 3
1 2
2 3
10
5
2 3 4 2 1
1 2
1 3
3 4
3 5
64
6
5 4 1 3 3 3
3 1
3 5
4 3
4 2
2 6
85

Hint

Explanation for Sample 1

The value of path 111 \to 1 is 11.

The value of path 121 \to 2 is 12=31⊕2=3.

The value of path 131 \to 3 is 123=01⊕2⊕3=0.

The value of path 222 \to 2 is 22.

The value of path 232 \to 3 is 23=12⊕3=1.

The value of path 333 \to 3 is 33.

The sum of the values of all paths is 1+3+0+2+1+3=101+3+0+2+1+3=10.

Constraints

For 30%30\% of the testdata, N200N \le 200.

For 50%50\% of the testdata, N1000N \le 1000.

For the other 20%20\% of the testdata, for every node x=1,2,,N1x = 1, 2, \cdots, N - 1, there is an edge between node xx and node x+1x + 1.

For 100%100\% of the testdata, 1aj,bjN1051 \le a_j, b_j \le N \le 10^5, and 0vi3×1060 \le v_i \le 3 \times 10^6.

Notes

This problem uses the original COCI scoring. The full score is 9090.

Translated from COCI2018-2019 CONTEST #2 T3 Deblo.

Translated by ChatGPT 5