Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)
E
Ancient Tree
Codeforces
2100
原题
← DF →
翻译中…

Bahamin came from the past to visit Ali — who came from the future. He also brought an ancient tree as a gift for Ali. He noticed some of its vertices have lost their color. Bahamin needs to repaint these vertices, but he is very busy with fixing his time machine. Fortunately (or unfortunately), dinosaurs now handle such tasks — for a fee, of course. He needs your help to find the coloring with minimum cost. So he gives you the problem as follows.

翻译中…

You are given a rooted tree$^{\text{∗}}$ of $n$ vertices, where vertex $1$ is the root. Each vertex has an integer weight $w_i$ and a color $c_i$, where the colors are integers between $1$ and $k$. However, some vertices have lost their colors, represented by $c_i = 0$.

翻译中…

We call vertex $v$ cutie if there exists two vertices $x$ and $y$, such that

翻译中…
  • $\operatorname{lca}(x, y)$$^{\text{†}}$ $= v$,
  • $c_x = c_y$, and
  • $c_x \neq c_v$.
翻译中…

The cost of the tree is the sum of weights of all cutie vertices.

翻译中…

You have to assign colors between $1$ and $k$ to all the vertices which have lost their colors. Find the minimum possible cost among all valid colorings and provide a coloring with the minimum possible cost.

翻译中…

$^{\text{∗}}$A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root. $^{\text{†}}$$\operatorname{lca}(x, y)$ denotes the lowest common ancestor (LCA) of $x$ and $y$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $k$ ($3 \leq n \leq 2 \cdot 10^5$, $2 \leq k \leq n$) — the number of vertices and the number of colors.

翻译中…

The second line contains $n$ integers $w_1,w_2,\ldots,w_n$ ($1 \leq w_i \leq 10^9$) — the weight of vertices.

翻译中…

The third line contains $n$ integers $c_1,c_2,\ldots,c_n$ ($0 \leq c_i \leq k$) — the color of vertices. $c_i=0$ means that vertex $i$ has lost its color.

翻译中…

Then $n−1$ lines follow, the $i$-th line containing two integers $u$ and $v$ ($1 \leq u, v \leq n$) — the two vertices that the $i$-th edge connects.

翻译中…

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 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, in the first line output a single integer — the minimum possible cost among all valid colorings.

翻译中…

In the second line output $n$ integers $c'_1, c'_2, \ldots, c'_n$ — a coloring with the minimum possible cost. You need to guarantee that:

翻译中…
  • $c'_i = c_i$ if $c_i \neq 0$;
  • $1 \leq c'_i \leq k$ if $c_i = 0$.
翻译中…

If there are multiple colorings with the minimum possible cost, you can output any of them.

翻译中…

Note

翻译中…

In the first test case, there are four choices for the missing color:

翻译中…
  • $c_2 = 1$ makes no vertex cutie, so the cost is $0$;
  • $c_2 = 2$ makes vertex $1$ cutie, since $c_2 = c_3 = 2$, $\operatorname{lca}(2, 3) = 1$ and $c_1 \neq 2$. Thus, the cost is $w_1 = 5$;
  • $c_2 = 3$ makes vertex $1$ cutie, since $c_2 = c_4 = 3$, $\operatorname{lca}(2, 4) = 1$ and $c_1 \neq 3$. Thus, the cost is $w_1 = 5$;
  • $c_2 = 4$ makes no vertex cutie, so the cost is $0$.
翻译中…

Thus, the minimum possible cost among different colorings is $0$.

翻译中…

In the second test case, every vertex has a color. So current cost is not changeable. And since we have $c_5 = c_2 = 2$, $\operatorname{lca}(2, 5) = 1$ and $c_1 \neq 2$, vertex $1$ is cutie and current cost is equal to $w_1 = 3$.

翻译中…

In the third test case, a possible coloring with the minimum cost is shown below:

翻译中…

image

翻译中…

Some other valid colorings are:

翻译中…
  • $c = [3, 1, 2, 2, 1, 2, 1, 2, 2, 1, 1]$ which makes vertices $1, 2, 3, 7$ cutie: All pairs of vertices mentioned have a different color from their LCA. So the cost will be $w_1 + w_2 + w_3 + w_7 = 11$.
  • $\operatorname{lca}(4, 8) = 1$;
    • $\operatorname{lca}(3, 4) = 2$;
    • $\operatorname{lca}(10, 11) = 3$;
    • $\operatorname{lca}(8, 9) = 7$.
  • $c = [3, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1]$ which makes vertices $1, 2, 7$ cutie: All pairs of vertices mentioned have a different color from their LCA. So the cost will be $w_1 + w_2 + w_7 = 7$.
  • $\operatorname{lca}(5, 7) = 1$;
    • $\operatorname{lca}(5, 11) = 2$;
    • $\operatorname{lca}(8, 9) = 7$.
翻译中…

It can be shown that no coloring with cost smaller than $7$ exists.

翻译中…

样例

Input #1
4
4 4
5 5 5 5
1 0 2 3
1 2
1 3
1 4
5 2
3 1 4 1 5
1 2 1 2 2
1 4
2 1
3 4
4 5
11 3
3 1 4 3 1 4 3 1 4 5 6
0 0 0 2 1 2 1 2 2 1 1
1 2
2 3
2 4
2 5
2 6
1 7
7 8
7 9
10 3
3 11
4 3
2 3 2 3
2 1 0 0
3 1
1 2
2 4
Output #1
0
1 4 2 3
3
1 2 1 2 2
7
2 3 1 2 1 2 1 2 2 1 1
0
2 1 3 1
InputCopy OutputCopy
题目信息
题号
2127E
时间限制
4 s
内存限制
256 MB
标签
构造
数据结构
DFS 及类似
并查集
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。