CF1882D.Tree XOR
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree with n vertices labeled from 1 to n . An integer ai is written on vertex i for i=1,2,…,n . You want to make all ai equal by performing some (possibly, zero) spells.
Suppose you root the tree at some vertex. On each spell, you can select any vertex v and any non-negative integer c . Then for all vertices i in the subtree † of v , replace ai with ai⊕c . The cost of this spell is s⋅c , where s is the number of vertices in the subtree. Here ⊕ denotes the bitwise XOR operation.
Let mr be the minimum possible total cost required to make all ai equal, if vertex r is chosen as the root of the tree. Find m1,m2,…,mn .
† Suppose vertex r is chosen as the root of the tree. Then vertex i belongs to the subtree of v if the simple path from i to r contains v .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤104 ). The description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ).
The second line of each test case contains n integers a1,a2,…,an ( 0≤ai<220 ).
Each of the next n−1 lines contains two integers u and v ( 1≤u,v≤n ), denoting that there is an edge connecting two vertices u and v .
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, print m1,m2,…,mn on a new line.
输入输出样例
输入#1
2 4 3 2 1 0 1 2 2 3 2 4 1 100
输出#1
8 6 12 10 0
说明/提示
In the first test case, to find m1 we root the tree at vertex 1 .
- In the first spell, choose v=2 and c=1 . After performing the spell, a will become [3,3,0,1] . The cost of this spell is 3 .
- In the second spell, choose v=3 and c=3 . After performing the spell, a will become [3,3,3,1] . The cost of this spell is 3 .
- In the third spell, choose v=4 and c=2 . After performing the spell, a will become [3,3,3,3] . The cost of this spell is 2 .
Now all the values in array a are equal, and the total cost is 3+3+2=8 .
The values m2 , m3 , m4 can be found analogously.
In the second test case, the goal is already achieved because there is only one vertex.