题目链接: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. Go语言实现:【剑指offer】翻转单词顺序列

    该题目来源于牛客网<剑指offer>专题. 例如,"student. a am I",正确的句子应该是"I am a student." Go语言实 ...

  2. Visual Studio 2015 配置 Python 环境

    Visual Studio 2015可以在安装时选择安装Python环境,首次使用VS2015执行python时需要配置环境变量: 配置VS2015的环境前需要先下载Python并安装: https: ...

  3. HTTP&HTTPS协议详解之HTTP篇

    一.HTTP简介 01.什么是HTTP HTTP(HyperText Transfer Protocol ,超文本传输协议),是一个基于请求与响应的,无状态的,应用层的协议,常基于TCP/IP协议传输 ...

  4. Redis中RDB和AOF持久化区别和联系

    RDB和AOF持久化   ​RDB持久化 RDB是什么? 原理是redis会单独创建(fork) 一个与当前进程一模一 样的子进程来进行持久化,这个子进程的所有数据(变量.环境变量,程序程序计数器等) ...

  5. expect 运行脚本文件 执行postgres数据库操作

    #!/bin/bash /usr/bin/expect << EOF spawn /usr/local/pgsql/bin/.sh expect "*postgres:" ...

  6. shell脚本 监控ps 不存在则重启

    监控 tomcat ,如果自动停止了,则重新启动 #!/bin/bash Start=/usr/local/apache-tomcat-8.0.24/bin/startup.sh Url=" ...

  7. vue路由+vue-cli实现tab切换

    第一步:搭建环境 安装vue-cli cnpm install -g vue-cli安装vue-router cnpm install -g vue-router使用vue-cli初始化项目 vue ...

  8. const与指针、引用

    const与指针类型 定义一个指针*p: const int* p = NULL; int const* p = NULL; int* const p = NULL; 上面两行定义完全等价,第三行则不 ...

  9. JavaScript-迭代器模式

    迭代器模式 顺序访问一个集合 使用者无需知道集合内部结构(封装) jQuery 示例 <!DOCTYPE html> <html> <head> <meta ...

  10. HTML连载70-相片墙、盒子阴影和文字阴影

    一. 制作一个相片墙 二. <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...