Xorability P43845


Statement
 

pdf   zip

Consider a tree with nn nodes numbered from 1 to nn, rooted at 1. Each node ii has a natural label i\ell_i. Given two nodes uu and vv, define X(u,v)X(u, v) as the exclusive or (^ in C++) of all the i\ell_i in the path from uu to vv.

Let LL be the set of leaves of the tree. Please compute vLX(1,v)\sum_{v \in L} X(1, v).

Input

Input consists of several trees, each with the number of nodes nn, followed by 1,,n\ell_1, \dots, \ell_n, followed by n1n - 1 pairs of nodes describing the edges of the tree. Assume 2n1052 \le n \le 10^5, and 0li1090 \le l_i \le 10^9.

Output

For each tree, print the required sum.

Public test cases
  • Input

    3
    2 7 3
    1 2  2 3
    6
    18 6 9 12 15 3
    1 6  6 5  3 6  2 3  3 4
    7
    1 2 3 4 5 6 7
    1 4  1 5  2 1  4 6  2 3  2 7
    4
    1000000000 0 0 0
    2 1  3 1  4 1
    

    Output

    6
    80
    11
    3000000000
    
  • Information
    Author
    Ángel García
    Language
    English
    Official solutions
    C++
    User solutions
    C++