CF1882D.Tree XOR

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices labeled from 11 to nn. An integer aia_{i} is written on vertex ii for i=1,2,…,ni = 1, 2, \ldots, n. You want to make all aia_{i} equal by performing some (possibly, zero) spells.

Suppose you root the tree at some vertex. On each spell, you can select any vertex vv and any non-negative integer cc. Then for all vertices ii in the subtree†^{\dagger} of vv, replace aia_{i} with ai⊕ca_{i} \oplus c. The cost of this spell is s⋅cs \cdot c, where ss is the number of vertices in the subtree. Here ⊕\oplus denotes the bitwise XOR operation.

Let mrm_r be the minimum possible total cost required to make all aia_i equal, if vertex rr is chosen as the root of the tree. Find m1,m2,…,mnm_{1}, m_{2}, \ldots, m_{n}.

†^{\dagger} Suppose vertex rr is chosen as the root of the tree. Then vertex ii belongs to the subtree of vv if the simple path from ii to rr contains vv.

给你一棵有 nn 个顶点的树,顶点编号为 11 到 nn。对每个 i=1,2,…,ni = 1, 2, \ldots, n,顶点 ii 上写有一个整数 aia_{i}。你可以执行若干次(可能为零次)法术,使得所有 aia_{i} 的值相等。

假设你将树以某个顶点为根进行定根。每次法术中,你可以任选一个顶点 vv 和一个非负整数 cc;然后对 vv 的子树†^{\dagger} 中的所有顶点 ii,将 aia_{i} 替换为 ai⊕ca_{i} \oplus c。该次法术的代价为 s⋅cs \cdot c,其中 ss 是该子树中顶点的个数。此处 ⊕\oplus 表示按位异或运算。

令 mrm_r 表示当以顶点 rr 为树根时,使所有 aia_i 相等所需的最小总代价。请计算 m1,m2,…,mnm_{1}, m_{2}, \ldots, m_{n}。

†^{\dagger} 假设选定顶点 rr 作为树根,则顶点 ii 属于顶点 vv 的子树,当且仅当从 ii 到 rr 的简单路径经过 vv。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^{4}). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^{5}).

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<2200 \le a_i \lt 2^{20}).

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n), denoting that there is an edge connecting two vertices uu and vv.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^{5}.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^{4})。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^{5})。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<2200 \le a_i \lt 2^{20})。

接下来的 n−1n-1 行中,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示顶点 uu 和 vv 之间存在一条边。

保证所给的边构成一棵树。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^{5}。

输出格式

For each test case, print m1,m2,…,mnm_1, m_2, \ldots, m_n on a new line.

对于每个测试用例,在新的一行上输出 m1,m2,…,mnm_1, m_2, \ldots, m_n。

输入输出样例

  • 输入#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 m1m_1 we root the tree at vertex 11.

  1. In the first spell, choose v=2v=2 and c=1c=1. After performing the spell, aa will become [3,3,0,1][3, 3, 0, 1]. The cost of this spell is 33.
  2. In the second spell, choose v=3v=3 and c=3c=3. After performing the spell, aa will become [3,3,3,1][3, 3, 3, 1]. The cost of this spell is 33.
  3. In the third spell, choose v=4v=4 and c=2c=2. After performing the spell, aa will become [3,3,3,3][3, 3, 3, 3]. The cost of this spell is 22.

Now all the values in array aa are equal, and the total cost is 3+3+2=83 + 3 + 2 = 8.

The values m2m_2, m3m_3, m4m_4 can be found analogously.

In the second test case, the goal is already achieved because there is only one vertex.

在第一个测试用例中,为求出 m1m_1,我们将树以顶点 11 为根。

  1. 在第一次施法中,选择 v=2v=2 和 c=1c=1。施法后,数组 aa 变为 [3,3,0,1][3, 3, 0, 1]。此次施法的代价为 33。
  2. 在第二次施法中,选择 v=3v=3 和 c=3c=3。施法后,数组 aa 变为 [3,3,3,1][3, 3, 3, 1]。此次施法的代价为 33。
  3. 在第三次施法中,选择 v=4v=4 和 c=2c=2。施法后,数组 aa 变为 [3,3,3,3][3, 3, 3, 3]。此次施法的代价为 22。

此时,数组 aa 中所有值均已相等,总代价为 3+3+2=83 + 3 + 2 = 8。

类似地可求出 m2m_2、m3m_3、m4m_4。

在第二个测试用例中,目标已自然达成,因为图中仅有一个顶点。

输入解题思路,AI测评打分。不知道怎么写?

首页