Color a Tree
题目链接:Click here
Solution:
看起来不太能dp,则考虑树上贪心
题目要求一个点必须先染父亲才能染自己,就给了我们启示,贪心的合并点
我们定义一个点的权重为这个点的价值和/点数,然后贪心的选择权重最大的点加入到答案,合并到他的父亲
值得注意的是加入到答案的过程为加上这个点的价值和\(\times\)父亲节点的点数,因为这代表在他之前染色的点数
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,root;
struct node{
int s,v,f;
double w;
}tree[1001];
int find_max(){
int m;double maxn=0;
for(int i=1;i<=n;i++)
if(i!=root&&tree[i].w>maxn)
maxn=tree[i].w,m=i;
return m;
}
int main(){
while(1){
scanf("%d%d",&n,&root);
if(n==0&&root==0)break;
int ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&tree[i].v);
tree[i].s=1;tree[i].w=double(tree[i].v);
ans+=tree[i].v;
}
for(int i=1;i<n;i++){
int a,b;scanf("%d%d",&a,&b);
tree[b].f=a;
}
for(int i=1;i<n;i++){
int now=find_max();
int fa=tree[now].f;
ans+=tree[now].v*tree[fa].s;
tree[now].w=0;
for(int j=1;j<=n;j++)
if(tree[j].f==now)tree[j].f=fa;
tree[fa].s+=tree[now].s;
tree[fa].v+=tree[now].v;
tree[fa].w=double(tree[fa].v)/tree[fa].s;
}printf("%d\n",ans);
}
return 0;
}
Color a Tree的更多相关文章
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- Color a Tree & 排列
Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...
- hdu 6241 Color a Tree 2017 CCPC 哈理工站 L
Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...
- HDU - 6241 :Color a Tree(不错的二分)
Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- HDU 1055 - Color a Tree
一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...
随机推荐
- redis缓存雪崩
缓存雪崩 缓存雪崩,是指在某一个时间段,缓存集中过期失效. 产生雪崩的原因之一,比如在写本文的时候,马上就要到双十二零点,很快就会迎来一波抢购,这波商品时间比较集中的放入了缓存,假设缓存一个小时.那么 ...
- CSS(下)
目录 CSS(下) CSS属性相关 宽和高 字体属性 背景属性 边框 border-radius display属性 CSS盒子模型 margin外边距 padding内填充 浮动(float) 限制 ...
- 怎么快速写好看的手机menu菜单
要达到这样的效果: <div class="menu"> <div class="menu-1"> <img alt=" ...
- Intellij idea启动项目提示"ClassNotFoundException"
引用至Intellij IDEA 启动项目ClassNotFoundException 使用Intellij IDEA的过程中,新创建的项目启动时报 严重: Error configuring app ...
- ftp服务器上传下载共享文件
1 windows下搭建ftp服务器 https://blog.csdn.net/qq_34610293/article/details/79210539 搭建好之后浏览器输入 ftp://ip就可以 ...
- C# 面向对象4 构造函数
构造函数 1.构造函数用来创建对象,并且可以在构造函数中对对象进行初始化. (给对象的每个属性依次的赋值) 2.构造函数是用来创建对象的特殊方法: 1.方法名和类名一样. 2.没有返回值,连void都 ...
- JVM - Java虚拟机规范官方文档
Java虚拟机规范官方文档
- Gcc 安装过程中部分配置
Gcc 安装过程中部分配置详解 全文参考<have fun with Gcc>一文,如有需要请联系原作者prolj@163.com 解压gcc源码后,需要进行configure,这时候一般 ...
- Linux 权限和目录更改、移除、更换目录、列出目录内容、使用通配符、移动、重命名
12 chgrp :改变档案.目录所属群组 chgrp -R dirname/filename chown :改变档案/目录拥有者 chown -R 账 ...
- 文件hash、上传,实现文件上传重复验证
在平台开发中,我们往往对性能要求十分严苛,每一个字段.接口都有严格的要求. 系统中文件流操作十分占用资源,这里为大家介绍对文件上传进行哈希校验---同一文件只允许上传一次到服务器,其他的上传只要指向文 ...