CF1244D.Paint the Tree

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree consisting of nn vertices. A tree is an undirected connected acyclic graph.

Example of a tree.

You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.

You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples (x,y,z)(x, y, z) such that x≠y,y≠z,x≠zx \neq y, y \neq z, x \neq z, xx is connected by an edge with yy, and yy is connected by an edge with zz. The colours of xx, yy and zz should be pairwise distinct. Let's call a painting which meets this condition good.

You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.

给你一棵包含 nn 个顶点的树。树是一种无向、连通且无环的图。

树的一个示例。

你需要将每个顶点染成三种颜色之一。对于每个顶点,你已知将其染成每种颜色的代价。

你需要对顶点进行染色,使得任意由恰好三个互不相同顶点构成的路径中,都不包含颜色相同的顶点。换言之,考虑所有三元组 (x,y,z)(x, y, z),满足 x≠yx \neq y,y≠zy \neq z,x≠zx \neq z,且 xx 与 yy 相邻、yy 与 zz 相邻。则 xx、yy、zz 的颜色必须两两不同。我们称满足该条件的染色方案为合法染色。

你需要计算合法染色的最小总代价,并构造出一种达到该最小代价的染色方案。若不存在合法染色方案,则报告这一点。

输入格式

The first line contains one integer nn (3≤n≤100 000)(3 \le n \le 100\,000) — the number of vertices.

The second line contains a sequence of integers c1,1,c1,2,…,c1,nc_{1, 1}, c_{1, 2}, \dots, c_{1, n} (1≤c1,i≤109)(1 \le c_{1, i} \le 10^{9}), where c1,ic_{1, i} is the cost of painting the ii-th vertex into the first color.

The third line contains a sequence of integers c2,1,c2,2,…,c2,nc_{2, 1}, c_{2, 2}, \dots, c_{2, n} (1≤c2,i≤109)(1 \le c_{2, i} \le 10^{9}), where c2,ic_{2, i} is the cost of painting the ii-th vertex into the second color.

The fourth line contains a sequence of integers c3,1,c3,2,…,c3,nc_{3, 1}, c_{3, 2}, \dots, c_{3, n} (1≤c3,i≤109)(1 \le c_{3, i} \le 10^{9}), where c3,ic_{3, i} is the cost of painting the ii-th vertex into the third color.

Then (n−1)(n - 1) lines follow, each containing two integers uju_j and vjv_j (1≤uj,vj≤n,uj≠vj)(1 \le u_j, v_j \le n, u_j \neq v_j) — the numbers of vertices connected by the jj-th undirected edge. It is guaranteed that these edges denote a tree.

第一行包含一个整数 nn (3≤n≤100 000)(3 \le n \le 100\,000) —— 顶点的数量。

第二行包含一个整数序列 c1,1,c1,2,…,c1,nc_{1, 1}, c_{1, 2}, \dots, c_{1, n} (1≤c1,i≤109)(1 \le c_{1, i} \le 10^{9}),其中 c1,ic_{1, i} 表示将第 ii 个顶点染成第一种颜色的代价。

第三行包含一个整数序列 c2,1,c2,2,…,c2,nc_{2, 1}, c_{2, 2}, \dots, c_{2, n} (1≤c2,i≤109)(1 \le c_{2, i} \le 10^{9}),其中 c2,ic_{2, i} 表示将第 ii 个顶点染成第二种颜色的代价。

第四行包含一个整数序列 c3,1,c3,2,…,c3,nc_{3, 1}, c_{3, 2}, \dots, c_{3, n} (1≤c3,i≤109)(1 \le c_{3, i} \le 10^{9}),其中 c3,ic_{3, i} 表示将第 ii 个顶点染成第三种颜色的代价。

接下来有 (n−1)(n - 1) 行,每行包含两个整数 uju_j 和 vjv_j (1≤uj,vj≤n,uj≠vj)(1 \le u_j, v_j \le n, u_j \neq v_j) —— 表示第 jj 条无向边所连接的两个顶点的编号。保证这些边构成一棵树。

输出格式

If there is no good painting, print −1-1.

Otherwise, print the minimum cost of a good painting in the first line. In the second line print nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤3)(1 \le b_i \le 3), where the ii-th integer should denote the color of the ii-th vertex. If there are multiple good paintings with minimum cost, print any of them.

如果不存在合法的染色方案,则输出 −1-1。

否则,在第一行输出合法染色方案的最小总代价。在第二行输出 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤3)(1 \le b_i \le 3),其中第 ii 个整数表示第 ii 个顶点的颜色。若存在多个总代价最小的合法染色方案,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    3 2 3
    4 3 2
    3 1 3
    1 2
    2 3

    输出#1

    6
    1 3 2
  • 输入#2

    5
    3 4 2 1 2
    4 2 1 5 4
    5 3 2 1 1
    1 2
    3 2
    4 3
    5 3

    输出#2

    -1
  • 输入#3

    5
    3 4 2 1 2
    4 2 1 5 4
    5 3 2 1 1
    1 2
    3 2
    4 3
    5 4

    输出#3

    9
    1 3 2 1 3

说明/提示

All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color 11, the second vertex — into color 33, and the third vertex — into color 22. The cost of this painting is 3+2+1=63 + 2 + 1 = 6.

第一个例子中,所有顶点必须涂成不同的颜色。最优的涂色方式是:将第一个顶点涂成颜色 11,第二个顶点涂成颜色 33,第三个顶点涂成颜色 22。该涂色方案的代价为 3+2+1=63 + 2 + 1 = 6。

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

首页