(思维+暴力)CodeForces - 1244 D Paint the Tree

思路:首先可以看出,这个树只能是一条链的形式,如果有分叉,那必然染色会有冲突,所以我们只要枚举前两个点颜色,

其他点就都已经确定了,纪录下最小值就好了

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.

Input

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.

Output

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.

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 1

, the second vertex — into color 3, and the third vertex — into color 2. The cost of this painting is 3+2+1=6.

#include<bits/stdc++.h>
using namespace std;
#define maxn 113050
#define ll long long
vector<ll>e[maxn];
ll d[maxn],p[maxn];
ll c[4][maxn],a[maxn];
struct node
{
    ll id,w;
};node b[maxn];
bool cmp(node a,node b)
{
    return a.id<b.id;
}
void dfs(ll step,ll u,ll fa)
{
    d[step]=u;
    for(auto v:e[u])
    {
        if(v==fa) continue;
        dfs(step+1,v,u);
    }
}
int main()
{
    ll n;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&c[1][i]);
    for(ll i=1;i<=n;i++) scanf("%lld",&c[2][i]);
    for(ll i=1;i<=n;i++) scanf("%lld",&c[3][i]);
    for(ll i=1;i<n;i++)
    {
        ll x,y;
        scanf("%lld %lld",&x,&y);
        p[x]++;p[y]++;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    ll root=0;
    for(ll i=1;i<=n;i++)
    {
        if(p[i]>2)
        {
            printf("-1\n");
            return 0;
        }
        if(p[i]==1) root=i;
    }
    dfs(1,root,0);ll ans=1e18;
    for(ll i=1;i<=n;i++) b[i].id=d[i];
    for(ll i=1;i<=3;i++)
    {
        for(ll j=1;j<=3;j++)
        {
            if(i==j) continue;
            ll w=c[i][d[1]]+c[j][d[2]];
            a[1]=i;a[2]=j;
            for(ll k=3;k<=n;k++)
            {
                a[k]=a[k-1]^a[k-2];
                w+=c[a[k]][d[k]];
            }
            if(ans>w)
            {
                ans=w;
                for(ll k=1;k<=n;k++) b[k].w=a[k];
            }
        }
    }
    printf("%lld\n",ans);
    sort(b+1,b+1+n,cmp);
    for(ll i=1;i<=n;i++)
    {
        printf("%lld",b[i].w);
        printf("%c",i==n?'\n':' ');
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43497140/article/details/106690707
今日推荐