题目链接: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的更多相关文章

  1. Codeforces Round #592 (Div. 2)

    A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...

  2. 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/ ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #592 (Div. 2) E

    给你一个数组,你最多可以进行k次操作,每次操作可以使一个数+1或者-1,问操作之后数组的极差最小可能是多少 利用map来模拟移动,可以观察到每次应该选择数量少的一组数让他们进行移动是最优的 int m ...

  7. Codeforces Round #592 (Div. 2)G(模拟)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 关于线段树的感悟(Segment Tree)

    线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...

  2. openssl 自签名证书SHA1加密算法

    openssl genrsa -out privkey.pem 2048 openssl req -new -key privkey.pem -sha1 -out cert.csr openssl r ...

  3. 设置允许外网访问MySQL

    1:设置mysql的配置文件     /etc/mysql/my.cnf      找到 bind-address  =127.0.0.1  将其注释掉://作用是使得不再只允许本地访问:  重启my ...

  4. [Redis-CentOS7]Redis打开远程连接(十) Could not connect to Redis at 127.0.0.1:6379: Connection refused

    通过网络无法访问Redis redis-cli 172.16.1.111 Could not connect to Redis at 127.0.0.1:6379: Connection refuse ...

  5. 意法半导体STM32MP157A MPU加持,米尔科技首款ST Linux开发板MYD-YA157C评测

    ST公司去年推出了MPU系列芯片,MPU系列不同于以往产品,它既包含有ARM公司Cortex M 单片机核心,也包含有ARM公司Cortex A 应用处理器核心,以期将STM32单片机产品优势扩展到更 ...

  6. Ubuntu下LAMP的环境配置教程

    总体来说,Ubuntu下安装LAMP环境是比较简单的,只需按照命令行执行即可,记录操作以备不时之需. 一,首先更新Ubuntu里面所有的软件 sudo apt-get update 二.之后安装Apa ...

  7. 多线程笔记 - disruptor

    disruptor 可以理解为一个生产消费的框架. 具体翻译教程: http://ifeve.com/disruptor-getting-started/ 这个框架从数据上看, 是很强大的. 号称1s ...

  8. 学习了解CSS3发展方向和CSS样式与优先级

    通过学习CSS3游戏介绍.CSS样式和优先级章节,了解到html5+css3+js不光可以实现动画,其次可以往这个游戏与建模方向发展,更多css3特效访问Gerard Ferrandez on Cod ...

  9. JAVA 增删改查接口命名规范(dao层与 service 层

    开发时,有很多规范,这里写的是命名规范. Dao 接口命名   insert batchInsert selectOne selectById count selectList update dele ...

  10. Android开发之adt bundle安装

    这个学期开了一门手机游戏开发的课,所以就接触到了adt bundle,Android开发环境有三种方式,分别是JDK+SDK+Eclipse+ADT.JDK+adt-bundle与JDK+Androi ...