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 ...
随机推荐
- 【JDBC】CRUD操作
JDBC的CRUD操作 向数据库中保存记录 修改数据库中的记录 删除数据库中的记录 查询数据库中的记录 保存代码的实现 package demo1; import java.sql.Connectio ...
- 服务器 vim模式下报错E37: No write since last change (add ! to override)
故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...
- 对于Python语音性能的一些个人见解
虽然运行速度慢是 Python 与生俱来的特点,大多数时候我们用 Python 就意味着放弃对性能的追求.但是,就算是用纯 Python 完成同一个任务,老手写出来的代码可能会比菜鸟写的代码块几倍,甚 ...
- element-ui中的hover 光标移入某一个具体的td 有hover效果
<template> <div> <el-table :data="tableData" style="width: 100%"& ...
- MYSQL ERROR:1130 解决
MYSQL ERROR:1130 解决 ERROR 1130: Host '127.0.0.7' is not allowed to connect to this MySQL server 解决 ...
- 发布Jar包到中央仓库
参考流程 https://blog.csdn.net/qq_36838191/article/details/81027586 备份还原 gpg-keys https://blog.rathena.c ...
- Linq分页排序通用方法
1.通用方法 2.调用 -----------------------------1.------------------------------------------- public class ...
- SqlServer,Oracle,db2,MySql查询表索引
SqlServer1. 查询表索引 SELECT 索引名称=a.name ,表名=c.name ,索引字段名=d.name ,索引字段位置=d.colid ,c.status FROM sysinde ...
- 在 ASP.NET Core 中使用 ApplicationPart 的简单示例
1. 项目截图: 2. 代码 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFra ...
- CAT 监控搭建
简介 CAT 是基于 Java 开发的实时应用监控平台,为美团点评提供了全面的实时监控告警服务. 已经在美团点评的基础架构中间件框架(MVC框架,RPC框架,数据库框架,缓存框架等,消息队列,配置系统 ...