Tree and XOR

5

2 votes
Algorithms, Depth First Search, Graphs, Medium
Problem

    

For a tree , rooted at node , having values at nodes, and a node , lets define as the bitwise xor of all values in the subtree of node .

You are given a tree . Let be the tree remaining after removing all nodes in subtree of . Define . You have to find sum of over all nodes .

Input:

First line contains a single integer , denoting the number of nodes in the tree . Next line contains space separated integers denoting the values of nodes in the tree , integer being the value of node $i$. Each of the next lines contain space separated integers and , meaning that there is an edge from node to node .

Output:

Print a single integer, the sum of for all nodes (except ) of the tree .

Constraints:

Sample Input
3
9 6 5
1 2
1 3
Sample Output
20
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

.

Editor Image

?