You are given a tree $$G$$ with $$N$$ nodes. The $$i^{th}$$ node of the tree has been assigned value $$A_i$$. We define $$d(u, v)$$ as the bitwise xor of weight of nodes present on the simple path between $$u$$ and $$v$$. You are required to find:
$$
\sum\limits_{i=1}^{N} \sum\limits_{j=i+1}^{N} d(i,j)
$$
Input format
- The first line contains a single integer $$T$$ denoting the number of test cases.
- The first line of each test case contains an integer $$N$$ that denotes the number of nodes in tree $$G$$.
- The second line of each test case contains $$N$$ space-separated integers denoting the initial weight on the nodes of the tree($$A_i$$).
- For each test case, $$(N-1)$$ lines follow. Each of these lines contains two space-separated integers $$u$$ and $$v$$ denoting that there is an edge between these two nodes.
Output format
For each test case(in a separate line), output the sum $$ \sum\limits_{i=1}^{N} \sum\limits_{j=i+1}^{N} d(i,j) $$, where $$d(i, j)$$ is the bitwise xor of weight of nodes present on the simple path between $$i$$ and $$j$$.
Constraints
$$ 1 \le T \le 1000 \\
1 \le N \le 10^5 \\
1 \le A_i \le 10^9 \\
\text{Sum of N over all test cases does not exceed } 10^6 $$
In the first test case, we have $$A_1 = 1, A_2 = 3, A_3 = 4$$. The tree looks as follows:
We have $$d(1, 2) = A_1 \oplus A_2 = 2$$, $$d(1, 3) = A_1 \oplus A_3 = 5$$, $$d(2, 3) = A_2 \oplus A_1 \oplus A_3 = 6$$. The sum is $$ d(1, 2) + d(1, 3) + d(2, 3) = 2 + 5 + 6 = 13$$ and we output it.
In the second test case, there is only one edge and we find $$d(1, 2) = A_1 \oplus A_2 = 2$$ and output it.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor