Codeforces 1244D. Paint the Tree
首先如果某个点的度数大于 $2$ 那么显然无解
然后考虑点的度数小于等于 $2$ 的情况
发现其实是一条链
一旦确定了链开头的两个点,后面的点的颜色都可以通过之前的点推出
所以直接枚举即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e5+;
const ll INF=1e18;
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int n,cst[N][],ans[][],col[N],pcol[N],du[N];
vector <int> V;
int main()
{
n=read();
for(int i=;i<;i++)
for(int j=;j<=n;j++) cst[j][i]=read();
for(int i=;i<n;i++)
{
int a=read(),b=read();
add(a,b); add(b,a);
du[a]++; du[b]++;
}
for(int i=;i<=n;i++)
if(du[i]>) { printf("-1\n"); return ; }
int rt=; for(int i=;i<=n;i++) if(du[i]==) { rt=i; break; }
int pre=rt,now=to[fir[rt]]; V.push_back(rt);
while(du[now]>)
for(int i=fir[now];i;i=from[i])
{
int &v=to[i]; if(v==pre) continue;
V.push_back(now); pre=now; now=v; break;
}
V.push_back(now);
ll ans=INF;
for(int i=;i<;i++)
for(int j=;j<;j++) if(i!=j)
{
int len=V.size(); ll res=cst[V[]][i]+cst[V[]][j];
pcol[V[]]=i; pcol[V[]]=j;
for(int k=;k<len;k++)
{
for(int c=;c<;c++)
if(c!=pcol[V[k-]]&&c!=pcol[V[k-]]) pcol[V[k]]=c;
res+=cst[V[k]][pcol[V[k]]];
}
if(res<ans)
for(int k=;k<len;k++) col[V[k]]=pcol[V[k]];
ans=min(ans,res);
}
printf("%lld\n",ans);
for(int i=;i<=n;i++) printf("%d ",col[i]+);
puts(""); return ;
}
Codeforces 1244D. Paint the Tree的更多相关文章
- Codeforces 1240C. Paint the Tree
传送门 首先每个点 $u$ 只能选择不超过 $k$ 个相连的边 并且设边为 $(u,v)$ ,那么此时 $v$ 也必须选择这条边 因为图是一颗树,显然考虑一下树形 $dp$ 设 $f[x][0/1]$ ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Paint the Tree
Paint the Tree 题目来源: Moscow Pre-Finals Workshop 2018 Day 5 C 题目大意: 一棵\(n(n\le2000)\)个点的树,有\(m(2<m ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- E. Paint the Tree 树形dp
E. Paint the Tree 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两个节点拥有同一种颜色,那么就说 这条边是饱和的,一个树的价值 ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 196C Paint Tree(贪心+极角排序)
题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...
随机推荐
- 关于使用express作为spa应用服务的问题
前端工程师应该知道,spa是基于前端路由的单页面应用,如果服务端不做相应的配置,会经常出现404的问题. 一般的做法是默认返回应用的首页. express // 安装相关依赖 npm install ...
- 使用git Bash Here 绑定账号密码错误后 无法自动重新绑定
新安装的git 要打开gitbash 运行下面两个命令:1 git config --global user.name "Your Name"2 git config --glob ...
- shell之列表的定义与循环
字符串列表定义方法1: a=(f1 f2 f3 f4)for i in ${a[*]}#遍历每一个列表值 for i in ${a[@]}#等价与上一句 实例: #!bin/basha=(f1 f2 ...
- pytest+allure展示环境信息
allure展示环境信息 要将信息添加到Environment小部件,只需在生成报告之前在目录中创建environment.properties(或environment.xml)文件allure-r ...
- linux 自动输入用户名和密码 ftp
参考文章: http://blog.sina.com.cn/s/blog_8709e3120101culd.html 方法概括; 1.echo管道 2. 利用重定向操作符command <&l ...
- java配置SSM框架下的redis缓存
pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...
- R语言与概率统计(一) 描述性统计分析
#查看已安装的包,查看已载入的包,查看包的介绍 ########例题3.1 #向量的输入方法 w<-c(75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 6 ...
- list中包含多个相同值时,List中的indexOf(Object o)方法的返回值
查一下Java API,List下的indexOf(Object o)方法的返回值为list列表中首个元素出现位置的索引,找不到时返回-1
- 刚开始使用idea的朋友,可以看一下下面这篇文章
刚开始使用idea的朋友,可以点击本链接看一下这篇文章 以及这些文章 http://www.jetbrains.com/help/idea/getting-help.html------ Gettin ...
- something about gdb
1 gdb 基础命令 b(break): 添加断点 r(run): 重头开始运行程序 n(next): 下一步 c(continue): 程序继续运行,直到下一处断点,或者程序运行到结束 q ...