CF1244D.Paint the Tree
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree consisting of n 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) such that x=y,y=z,x=z , x is connected by an edge with y , and y is connected by an edge with z . The colours of x , y and z 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.
输入格式
The first line contains one integer n (3≤n≤100000) — the number of vertices.
The second line contains a sequence of integers c1,1,c1,2,…,c1,n (1≤c1,i≤109) , where c1,i is the cost of painting the i -th vertex into the first color.
The third line contains a sequence of integers c2,1,c2,2,…,c2,n (1≤c2,i≤109) , where c2,i is the cost of painting the i -th vertex into the second color.
The fourth line contains a sequence of integers c3,1,c3,2,…,c3,n (1≤c3,i≤109) , where c3,i is the cost of painting the i -th vertex into the third color.
Then (n−1) lines follow, each containing two integers uj and vj (1≤uj,vj≤n,uj=vj) — the numbers of vertices connected by the j -th undirected edge. It is guaranteed that these edges denote a tree.
输出格式
If there is no good painting, print −1 .
Otherwise, print the minimum cost of a good painting in the first line. In the second line print n integers b1,b2,…,bn (1≤bi≤3) , where the i -th integer should denote the color of the i -th vertex. If there are multiple good paintings with minimum cost, print any of them.
输入输出样例
输入#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 1 , the second vertex — into color 3 , and the third vertex — into color 2 . The cost of this painting is 3+2+1=6 .