题意

https://vjudge.net/problem/CodeForces-1244D

有一棵树,有3种颜色,第i个节点染成第j种颜色的代价是c(i,j),现在要你求出一种染色方案,使得总代价最小,且对于任意三个相邻的节点,颜色不能相同。输出最小代价与其中一种方案。无解输出-1。

思路

首先可以发现当一个点的度数为3,那么它连的三个点的颜色必须互不相同,这样就把三种三色用完了,这个点就染不了了,于是如果存在度大于等于3的点,那么无解。

那么有解的树可以伸直成一条链,我们暴力枚举任意相邻的三个点的颜色,有3!种,然后我们暴力dfs从这三点的两端向外延伸即可。

复杂度O(6*n)

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=1e5+5;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll c[4][N];
ll du[N],col[N],rec[N];
vector<int>g[N];
int flag=0,s,a,b;
void dfs(int u,int fa)
{
int sz=g[u].size();
for(int i=0; i<sz; i++)
{
int v=g[u][i];
if(v==fa)
continue;
for(int i=1; i<=3; i++)
{
if(col[fa]!=i&&col[u]!=i)
{
col[v]=i;
break;
}
}
dfs(v,u);
}
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1; i<=3; i++)
{
for(int j=1; j<=n; j++)
{
cin>>c[i][j];
}
}
for(int i=0; i<n-1; i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
du[u]++,du[v]++;
if(du[u]==2)
{
s=u;
}
if(du[v]==2)
s=v;
if(du[u]>=3||du[v]>=3)
{
flag=1;
}
} if(flag)
{
cout<<-1<<endl;
return 0;
}
a=g[s][0],b=g[s][1];
ll ans=1e16;
for(int i=1; i<=3; i++)
{
for(int j=1; j<=3; j++)
{
for(int k=1; k<=3; k++)
{
if(i==j||j==k||i==k) continue;
for(int i=1; i<=n; i++)
col[i]=0;
col[s]=i,col[a]=j,col[b]=k;
dfs(a,s);
dfs(b,s);
ll sum=0;
for(int i=1; i<=n; i++)
{
sum+=c[col[i]][i];
}
if(sum<ans)
{
ans=sum;
for(int i=1; i<=n; i++)
rec[i]=col[i];
}
}
}
}
cout<<ans<<endl;
for(int i=1; i<=n; i++)
cout<<rec[i]<<" ";
cout<<endl;
return 0;
}

  

CodeForces - 1244D (思维+暴力)的更多相关文章

  1. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)

    题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...

  2. CodeForces - 1248D1 (思维+暴力)

    题意 有一个括号序列,你可以选择两个位置i,j(i可以等于j),进行交换.使得最后的循环位置(i的数目)最大. 循环位置:i(0<=i<len),将前i个字符移到最后,得到的新序列是合法的 ...

  3. Nice Garland CodeForces - 1108C (思维+暴力)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  4. CodeForces - 593A -2Char(思维+暴力枚举)

    Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is th ...

  5. CodeForces - 1230C(思维/暴力)

    题意 https://vjudge.net/problem/CodeForces-1230C 给了你总共有21张多米诺骨牌,每张牌有两个面,然后给你一个无向图,保证没有环和一个顶点多条边的情况存在.现 ...

  6. CodeForces - 1228D (暴力+思维+乱搞)

    题意 https://vjudge.net/problem/CodeForces-1228D 有一个n个顶点m条边的无向图,在一对顶点中最多有一条边. 设v1,v2是两个不相交的非空子集,当满足以下条 ...

  7. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

  8. Karen and Game CodeForces - 816C (暴力+构造)

    On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...

  9. Chladni Figure CodeForces - 1162D (暴力,真香啊~)

    Chladni Figure CodeForces - 1162D Inaka has a disc, the circumference of which is nn units. The circ ...

随机推荐

  1. 【三】Gradle中的Task

    gradle中,最经常被使用的,一个task,一个是dependencies 1.Task声明 task默认是DefaultTask类, Task中有两个属性 group description,最佳 ...

  2. Azure 架构师认证考试

    Azure认证路线 AZ-300 AZ-301 https://blog.csdn.net/pg_edb/article/details/86794505 免费题 https://iteablue.c ...

  3. MSSQL 字段分组拼接

    方法1:缺点,不去重,不去空:见表1 with t as( select 'A' parent, 'A1' child union all select 'A', 'A1' union all sel ...

  4. Shell—输入输出重定向

    大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...

  5. Linux-3.14.12内存管理笔记【建立内核页表(1)】

    前面已经分析过了Intel的内存映射和linux的基本使用情况,已知head_32.S仅是建立临时页表,内核还是要建立内核页表,做到全面映射的.下面就基于RAM大于896MB,而小于4GB ,切CON ...

  6. Python升级PIP

    用pip list的时候.发现最后有两行黄颜色的.提示你可以用‘python -m pip install --upgrade pip’升级你的pip 当时我直接就复制粘贴上去运行了.但是报了一堆红的 ...

  7. acwing 600. 仰视奶牛

    题目地址  https://www.acwing.com/problem/content/description/602/ 约翰有N头奶牛,编号为1到N. 现在这N头奶牛按编号从小到大的顺序站成了一排 ...

  8. python 编程2

    一.课堂练习 描述 使用input输入若干个数,输出个数以及其中最大的数 1.普通方法实现 def max(*a): m=a[0] b=0 for x in a: if x>m: m=x b+= ...

  9. Linux驱动中的platform总线分析

    copy from :https://blog.csdn.net/fml1997/article/details/77622860 概述 从Linux2.6内核起,引入一套新的驱动管理和注册机制:pl ...

  10. 从Python安装到语法基础,这才是初学者都能懂的爬虫教程

    Python和PyCharm的安装:学会Python和PyCharm的安装方法 变量和字符串:学会使用变量和字符串的基本用法 函数与控制语句:学会Python循环.判断语句.循环语句和函数的使用 Py ...