#ds1003. Strange Memory

Strange Memory

Strange Memory

题目来源

CCPC Changchun 2020 F

题目描述

Once there was a rooted tree. The tree contained nn nodes, which were numbered 1,2,…,n1, 2, \dots, n. The node numbered 11 was the root of the tree. Besides, every node ii was assigned a number aia_i. Your were surprised to find that there were several pairs of nodes (i,j)(i, j) satisfying $$a_i \oplus a_j = a_{\mathrm{lca}(i,j)}, $$ where ⊕\oplus denotes the bitwise XOR operation, and lca(i,j)\mathrm{lca}(i,j) is the lowest common ancestor of ii and jj, or formally, the lowest (i.e. deepest) node that has both ii and jj as descendants.

Unfortunately, you cannot remember all such pairs, and only remember the sum of i⊕ji\oplus j for all different pairs of nodes (i,j)(i, j) satisfying the above property. Note that (i,j)(i, j) and (j,i)(j, i) are considered the same here. In other words, you will only be able to recall

$$\sum_{i=1}^n \sum_{j=i+1}^n \left[ a_i \oplus a_j = a_{\mathrm{lca}(i,j)} \right](i \oplus j).$$

You are assumed to calculate it now in order to memorize it better in the future.

输入格式

The first line contains a single integer n(2≤n≤105)n (2 \leq n \leq 10^5).

The second line contains nn integers, a1,a2,…,an(1≤ai≤106)a_1, a_2, \dots, a_n (1\leq a_i \leq 10^6).

Each of the next n−1n−1 lines contains 22 integers uu and vv (1≤u,v≤n,u≠v1\leq u, v\leq n, u\neq v), indicating that there is an edge between uu and vv. It is guaranteed that these edges form a tree.

输出格式

Print what you will memorize in the future.

样例输入

6
4 2 1 6 6 5
1 2
2 3
1 4
4 5
4 6

样例输出

18