[Codeforces 1244D]Paint the Tree(枚举)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_33831360/article/details/102538075

 显然一个点度超过2,那么这4个点颜色都不同,无解

所以度都为1,2。树退化成链。

确定了第1,2个,第3个点也唯一确定了(不和1,2的一样),那第4个也出来了。。。。。

所以两个点就能确定所有点。枚举前两个点颜色就行

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
 
using namespace std;

typedef long long LL;
#define int long long
#define N 201000
int c[3][N],st[N],v[N],n;//,f[N][3][3];
vector<int> g[N];
int du[N];
  
LL ans;  
int xx = -1,yy,zz;

void pd(int a,int b,int cc) {
	a--;b--;cc--;
	LL tp = 0;
	for (int i = 1; i <= n; i++) {
		int t = st[i];
		if (i%3 == 1) tp += c[a][t];
		if (i%3 == 2) tp += c[b][t];
		if (i%3 == 0) tp += c[cc][t];
	}
	if (xx == -1 || tp < ans) {
		ans = tp; xx = a; yy = b; zz = cc;
	}
}
  
main() {
	cin >> n;
 	for (int k = 0; k < 3; k++)
	  for (int i = 1; i <= n; i++)
	   cin >> c[k][i];
	for (int i = 1; i < n; i++) {
		int u,v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
		du[u]++; du[v]++;
	}
	int s;
	for (int i = 1; i <= n; i++)
	  if (du[i] >= 3) {
	  	puts("-1");
	  	return 0;
	  } else if(du[i] == 1) s = i;
	st[1] = s;
	int p = s; s = g[s][0];
	for (int i = 2; i <= n; i++) {
	    st[i] = s;
		int tp = s;
		s = (g[s][0]==p?g[s][1]:g[s][0]);
		p = tp;
	} 
	pd(1,2,3);pd(1,3,2);pd(2,1,3);pd(2,3,1);pd(3,1,2);pd(3,2,1);
    cout << ans << "\n";
//for (int i = 1; i <= n; i++) cout << st[i] << " ";
	for (int i = 1; i <= n; i++) {
		int t = st[i];
		if (i%3 == 1) v[t] = xx;
		if (i%3 == 2) v[t] = yy;
		if (i%3 == 0) v[t] = zz;
	}
	for (int i = 1; i <= n; i++) cout << v[i]+1 << " ";
    return 0;
}

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≠y,y≠z,x≠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.

Input

The first line contains one integer nn (3≤n≤100000)(3≤n≤100000) — the number of vertices.

The second line contains a sequence of integers c1,1,c1,2,…,c1,nc1,1,c1,2,…,c1,n (1≤c1,i≤109)(1≤c1,i≤109), where c1,ic1,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,nc2,1,c2,2,…,c2,n (1≤c2,i≤109)(1≤c2,i≤109), where c2,ic2,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,nc3,1,c3,2,…,c3,n (1≤c3,i≤109)(1≤c3,i≤109), where c3,ic3,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 ujuj and vjvj (1≤uj,vj≤n,uj≠vj)(1≤uj,vj≤n,uj≠vj) — the numbers of vertices connected by the jj-th undirected edge. It is guaranteed that these edges denote a tree.

Output

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,…,bnb1,b2,…,bn (1≤bi≤3)(1≤bi≤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.

Examples

input

Copy

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

output

Copy

6
1 3 2 

input

Copy

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

output

Copy

-1

input

Copy

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

output

Copy

9
1 3 2 1 3 

Note

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.

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/102538075
今日推荐