CodeForces - 1244D Paint the Tree

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

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

Output

6
1 3 2 

Input

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

-1

Input

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

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.

 

题意:总共有三种颜色,x,y,z,有n个点,对于每种颜色在每个点染色的花费给出,以及各点的连接情况知道,相邻的 

三个点的颜色不能一样,问在最少花费的条件下,每个点染什么颜色且满足要求。

思路:由于相连三个点的颜色不能一样,所以每个 点的度不能超过2,由此从树转化成了一条链,所以只要确定前两个点的颜色后面的点都确定,共有6种情况。x,y,z(0,1,2)

0,1,2

0,2,1

1,2,0

1,0,2

2,1,0

2,0,1

所以 找到这条链 记录好路径比较这6种 情况,求最小花费 下各点的染色情况。

存各点的连接关系时,因为数据多,用邻接表或vector容器都可以。

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
vector<int>e[N];//存图
vector<int>path;//记录路径
bool book[N];//标记是否走过
int cost[5][N];//每一点的花费
int color[N];//颜色分配
int col[10][5]= {{0,1,2},{0,2,1},{1,2,0},{1,0,2},{2,1,0},{2,0,1}};
void dfs(int x)
{
    book[x]=1;
    path.push_back(x);
    for(int i=0; i<e[x].size(); i++)
    {
        int xx=e[x][i];
        if(!book[xx])
            dfs(xx);
    }
}
int main()
{
    int n;
    scanf("%d",&n);

    for(int i=0; i<3; i++)
        for(int j=1; j<=n; j++)
            scanf("%d",&cost[i][j]);

    for(int i=1; i<n; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
    {
        if(e[i].size()>2)
        {
            printf("-1\n");
            return 0;
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(e[i].size()==1)
        {
            dfs(i);
            break;
        }
    }
    ll sum=0;
    int id;
    ll minn=1e17;
    for(int i=0; i<6; i++)
    {
        sum=0;
        for(int j=0; j<path.size(); j++)
        {
            sum+=cost[col[i][j%3]][path[j]];
        }
        if(sum<minn)
        {
            minn=sum;
            id=i;
        }
    }
    for(int i=0; i<n; i++)
        color[path[i]]=col[id][i%3]+1;


    printf("%lld\n",minn);
    for(int i=1; i<=n; i++)
    {
        if(i==1)
            printf("%d",color[i]);
        else
            printf(" %d",color[i]);
    }
    printf("\n");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值