CodeForces - 1244D (思维+暴力)
题意
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 (思维+暴力)的更多相关文章
- codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)
题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...
- CodeForces - 1248D1 (思维+暴力)
题意 有一个括号序列,你可以选择两个位置i,j(i可以等于j),进行交换.使得最后的循环位置(i的数目)最大. 循环位置:i(0<=i<len),将前i个字符移到最后,得到的新序列是合法的 ...
- Nice Garland CodeForces - 1108C (思维+暴力)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- CodeForces - 593A -2Char(思维+暴力枚举)
Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is th ...
- CodeForces - 1230C(思维/暴力)
题意 https://vjudge.net/problem/CodeForces-1230C 给了你总共有21张多米诺骨牌,每张牌有两个面,然后给你一个无向图,保证没有环和一个顶点多条边的情况存在.现 ...
- CodeForces - 1228D (暴力+思维+乱搞)
题意 https://vjudge.net/problem/CodeForces-1228D 有一个n个顶点m条边的无向图,在一对顶点中最多有一条边. 设v1,v2是两个不相交的非空子集,当满足以下条 ...
- Nikita and string [思维-暴力] ACM
codeforces Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes O ...
- 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 ...
- Chladni Figure CodeForces - 1162D (暴力,真香啊~)
Chladni Figure CodeForces - 1162D Inaka has a disc, the circumference of which is nn units. The circ ...
随机推荐
- Kafka学习(一)
官网 kafka.apache.org 集群部署 消息中间键 --> 分布式流式平台 Kafka Streaming Flume: 1个进程包含三个角色 source channle sink ...
- MySQL学习——管理用户权限
MySQL学习——管理用户权限 摘要:本文主要学习了使用DCL语句管理用户权限的方法. 了解用户权限 什么是用户 用户,指的就是操作和使用MySQL数据库的人.使用MySQL数据库需要用户先通过用户名 ...
- Python—包管理工具与上传工具
https://blog.csdn.net/libbyandhelen/article/details/78808959 https://www.cnblogs.com/nineep/p/947529 ...
- 昨天周末晚上没有出去,码了一小段,先留着kangkang。
昨天周末晚上没有出去,码了一小段,先留着kangkang. import numpy as npimport matplotlibmatplotlib.use('Agg')import matplot ...
- 《转》sql处理百万级以上的数据提高查询速度的方法
处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考 ...
- Python自动群发邮件,只需20行代码!
今日分享 Python自动群发邮件 import smtplib from email import (header) from email.mime import (text, applicatio ...
- vscode相关设置
一.如何设置vs每次点击文件打开新的窗口: 1.打开设置 2.搜索:"workbench.editor.enablePreview" 3.设置"workbench.edi ...
- Saiku上线部署准备(三十)
Saiku上线部署准备 零零散散琢磨了快5个月了,终于快要上线了哈哈哈哈哈..... 激动!!! 以下是本地打包编译saiku至部署到服务器上使用的完整步骤哦 saiku部署到服务器 源码编译需要注 ...
- EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks
增加模型精度的方法有增加网络的深度,特征图的通道数以及分辨率(如下图a-d所示).这篇文章研究了模型缩放,发现仔细平衡网络的深度.宽度和分辨率可以获得更好的性能(下图e).在此基础上,提出了一种新的缩 ...
- Linux网络——配置防火墙的相关命令
Linux网络——配置防火墙的相关命令 摘要:本文主要学习了如何在Linux系统中配置防火墙. iptables命令 iptables准确来讲并不是防火墙,真正的防火墙是运行于系统内核中的netfil ...