Tree and XOR

5

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

    

For a tree X, rooted at node 1, having values at nodes, and a node i, lets define S(X,i) as the bitwise xor () of all values in the subtree of node i.

You are given a tree T. Let Ti be the tree remaining after removing all nodes in subtree of i. Define P(i)=maxjTi(S(T,i)S(Ti,j)). You have to find sum of P(i) over all nodes i1.

Input:

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

Output:

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

Constraints:

2N2×105

1A,BN

1Gi109

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

P2=P3=10.

Editor Image

?