Codeforces Round #592 (Div. 2) 1224D

http://codeforces.com/problemset/problem/1244/D

暴力为一条链染色

/*
 * @Author: CY__HHH
 * @Date: 2019-10-12 12:39:18
 * @LastEditTime: 2019-10-15 15:06:18
 */
#include<bits/stdc++.h>
#define llinf (0x3f3f3f3f3f3f3f3f)
#define inf (0x3f3f3f3f)
typedef long long i64;
using namespace std;
const int maxn = 1e5 + 32;
vector<int> c[3];
vector<int> Grape[maxn];
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0),cout.tie(0);
    int n,u,v;  cin>>n;
    for(int i=0;i!=3;++i)
    {
        c[i].resize(n);
        for(int j=0;j!=n;++j)
            cin>>c[i][j];
    }
    for(int i=0;i!=n-1;++i)
    {
        cin>>u>>v;
        --u,--v;
        Grape[u].push_back(v);
        Grape[v].push_back(u);
    }
    for(int i=0;i!=n;++i)
        if(Grape[i].size() > 2)
        {
            cout<<-1<<'\n';
            return 0;
        }
    int s = 0,t = 0;
    while(Grape[s].size()==2)   ++s;
    t = Grape[s][0];
    vector<int> cnt(n),seq,seqEnd(n);
    seq.push_back(s),seq.push_back(t);
    while(Grape[t].size()==2)
    {
        s = s == Grape[t][0] ? Grape[t][1] : Grape[t][0];
        swap(s,t);
        seq.push_back(t);
    }//finish
    i64 sum = 0,minn = llinf;
    vector<int> col(3);
    for(int i=0;i!=3;++i)
    {
        for(int j=0;j!=3;++j)
        {
            if(i==j)    continue;
            sum = 0;
            col = {i,j,3^i^j};
            for(int k=0;k!=n;++k)
            {
                cnt[seq[k]] = col[k%3];
                sum += c[col[k%3]][seq[k]];
            }
            if(sum < minn)
            {
                minn = sum;
                seqEnd = cnt;
            }
        }
    }
    cout<<minn<<'\n';
    for(int i=0;i!=n;++i)
        cout<<seqEnd[i] + 1<<" ";
}

  

posted on 2019-10-15 17:29  chengyulala  阅读(105)  评论(0编辑  收藏  举报

导航