#ds1003. Strange Memory
Strange Memory
Strange Memory
题目来源
CCPC Changchun 2020 F
题目描述
Once there was a rooted tree. The tree contained nodes, which were numbered . The node numbered was the root of the tree. Besides, every node was assigned a number . Your were surprised to find that there were several pairs of nodes satisfying $$a_i \oplus a_j = a_{\mathrm{lca}(i,j)}, $$ where denotes the bitwise XOR operation, and is the lowest common ancestor of and , or formally, the lowest (i.e. deepest) node that has both and as descendants.
Unfortunately, you cannot remember all such pairs, and only remember the sum of for all different pairs of nodes satisfying the above property. Note that and 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 .
The second line contains integers, .
Each of the next lines contains integers and (), indicating that there is an edge between and . 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