Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D
题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是如果两个节点相邻,那么与这两个节点相邻的节点的颜色得和这两个节点都不一样)。这里给出每个节点染成三种颜色的代价,让我们求全部染色代价最小的方案与最小代价。
解析:如果一个点的度数大于等于3,那么肯定没有方案。所有这个树一定只是一条链,我们从度数为1的点开始dfs,把这条链存在数组里,就相当于我们只需要把数组连接三个数染成不同颜色需要付出的最小方案即可。由于情况只有3!=6种,我们可以全排列枚举,然后取最小值即可。
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+;
ll c[][N];
int deg[N],col[N];
vector<int>adj[N],vts;
int res[N];
void dfs(int s,int p=-)
{
vts.push_back(s);
for(auto v:adj[s])
{
if(v!=p)
{
dfs(v,s);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=;i<;i++)
{
for(int j=;j<n;j++)
{
cin>>c[i][j];
}
}
for(int i=;i<n-;i++)
{
int x,y;
cin>>x>>y;
x--;y--;
deg[x]++;
deg[y]++;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i=;i<n;i++)
{
if(deg[i]>=)
{
cout<<"-1\n";
return ;
}
}
for(int i=;i<n;i++)
{
if(deg[i]==)
{
dfs(i);
break;
}
}
ll ans=LLONG_MAX;
vector<int>p={,,},be;
do{
ll cur=;
for(int i=;i<n;i++)
{
cur+=c[p[i%]][vts[i]];
}
if(cur<ans)
{
ans=cur;
be=p;
}
}while(next_permutation(p.begin(),p.end()));
cout<<ans<<"\n"; for(int i=;i<n;i++)
{
res[vts[i]]=be[i%];
}
for(int i=;i<n;i++)
{
cout<<res[i]+<<" ";
}
return ;
}
Codeforces Round #592 (Div. 2) D - Paint the Tree的更多相关文章
- Codeforces Round #592 (Div. 2)
A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...
- Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...
- Codeforces Round #382 (Div. 2)E. Ostap and Tree
E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #592 (Div. 2)【C题】{补题ING}
思路:x,y,z肯定不为负数xw+dy=p,直接枚举系数较小的y即可,y的范围:y<w,因为大于w的时候,不如去增加x,这样x+y的和还能保持尽可能小. /* x*w+y*d=p; x*w+(K ...
- Codeforces Round #592 (Div. 2) E
给你一个数组,你最多可以进行k次操作,每次操作可以使一个数+1或者-1,问操作之后数组的极差最小可能是多少 利用map来模拟移动,可以观察到每次应该选择数量少的一组数让他们进行移动是最优的 int m ...
- Codeforces Round #592 (Div. 2)G(模拟)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
随机推荐
- 用反射、泛型 改造SqlHelper
1. 数据准备 public class BaseModel { public int Id { set; get; } } public class Company : BaseModel { p ...
- 全局new和宏结合起来的一个小应用
#include <iostream> using namespace std; void* operator new(size_t size, const char* file, int ...
- Spring监听器---ApplicationListener
说到事件监听,想到的肯定是观察者模式.但是这儿主要说下spring中的监听是怎么样的流程. 这就不得不说到spring容器的refresh方法,容器启动过程中,listener相关的主要是这个三个方法 ...
- javascript Math对象 常用数字操作方法
var t='1.2'; parseInt(t) parseFloat(t)//1.2 Number(1.2)//1.2强制转换为数字 2.向上取整,有小数就整数部分加1 js: Math.ceil( ...
- 2019SACC中国系统架构师大会 day1总结
早上:一. 爱奇艺的大数据中台战略: a) 介绍爱奇艺的产业文化.b) 通过大中台,可以进行部分数据的沉淀,用于后续的分析处理等等.. 从数据中台和业务中台,建立一种“苹果园“的生态系统.从原始的长视 ...
- 编写windows服务程序
2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...
- [Redis-CentOS7]Redis哈希操作(五)
哈希相当于下面Python代码 { "person":{ "name": "peigy", "age": "1 ...
- OSCP收集
推荐书籍: 渗透测试:黑客动手入门(+强烈推荐初学者) 黑客:剥削的艺术,第二版 Rtfm:Red Team Field手册 Web应用程序黑客手册:查找和利用安全漏洞 黑客手册:实用指南渗透测试 基 ...
- Emmet:HTML/CSS代码快速编写
html缩写: 1. 初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签.比如输入“ ...
- 用PHP&JS实现的ID&密码校验程序
声明:本程序纯粹是本人在学习过程中突发奇想做的,并未考虑任何可行性,实用性,只是留下来供以后参考. 前端页面 sign.html <!DOCTYPE html> <html> ...