题意

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. Linux iotop工具简介

    iotop的简介: iotop是一款开源.免费的用来监控磁盘I/O使用状况的类似top命令的工具,iotop可以监控进程的I/O信息.它是Python语言编写的,与iostat工具比较,iostat是 ...

  2. LeetCode 200. 岛屿数量

    习题地址 https://leetcode-cn.com/problems/number-of-islands/ 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水 ...

  3. JavaScript中一个对象数组按照另一个数组排序

    JavaScript中一个对象数组按照另一个数组排序 需求:排序 const arr1 = [33, 11, 55, 22, 66]; const arr2 = [{age: 55}, {age: 2 ...

  4. BZOJ2339/LG3214 「HNOI2011」 卡农 组合数学

    问题描述 BZOJ2339 本题的一些心得 对于这种无序集合计数类问题,可以通过对方案数除以某个数的阶乘,使得无序化变为有序化. 设计DP方程时候,应该先有序的列出状态转移方程每一项的来源,并一项项推 ...

  5. Python 列表生成式 & 字典生成式

    Python 列表生成式 & 字典生成式 通过生成式可以更加简洁地生成列表和字典 列表生成式 对比 直接生成数据后加入列表示例: user_list = list() for i in ran ...

  6. 微信小程序开发练习

    微信小程序开发工具git管理 https://blog.csdn.net/qq_36672905/article/details/82887102 这个开发工具的界面和交互真的是熟悉又友好,吹爆他

  7. Codeforces Round #599 (Div. 1) C. Sum Balance 图论 dp

    C. Sum Balance Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to ...

  8. x86-64数据格式、通用寄存器与操作数格式

    x86-64数据格式.通用寄存器与操作数格式 数据格式 ​ Intel用术语"字(word)"表示16位数据类型,32位为"双字(double words)", ...

  9. Zookeeper中Watcher监听实现增删改

    8.1 连接方法 package com.zookeeper.day01; import org.apache.zookeeper.*; import java.io.IOException; pub ...

  10. linux与ubuntu下vsftp的安装使用

    vsftp工具是linux与类linux系统上常用的ftp传输工具,按百度上的说法,它的不同点与好处有九点,不明觉厉,有兴趣的可以深入验证: 一.它是一个安全.高速.稳定的FTP服务器: 二.它可以做 ...