#P1882D. Tree XOR
Tree XOR
No submission language available for this problem.
Description
You are given a tree with vertices labeled from to . An integer is written on vertex for . You want to make all equal by performing some (possibly, zero) spells.
Suppose you root the tree at some vertex. On each spell, you can select any vertex and any non-negative integer . Then for all vertices in the subtree of , replace with . The cost of this spell is , where is the number of vertices in the subtree. Here denotes the bitwise XOR operation.
Let be the minimum possible total cost required to make all equal, if vertex is chosen as the root of the tree. Find .
Suppose vertex is chosen as the root of the tree. Then vertex belongs to the subtree of if the simple path from to contains .
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
The second line of each test case contains integers ().
Each of the next lines contains two integers and (), denoting that there is an edge connecting two vertices and .
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, print on a new line.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
The second line of each test case contains integers ().
Each of the next lines contains two integers and (), denoting that there is an edge connecting two vertices and .
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print on a new line.
Note
In the first test case, to find we root the tree at vertex .
- In the first spell, choose and . After performing the spell, will become . The cost of this spell is .
- In the second spell, choose and . After performing the spell, will become . The cost of this spell is .
- In the third spell, choose and . After performing the spell, will become . The cost of this spell is .
Now all the values in array are equal, and the total cost is .
The values , , can be found analogously.
In the second test case, the goal is already achieved because there is only one vertex.