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

  1. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  2. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. Color a Tree HDU - 6241

    /* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...

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

  5. Color a Tree & 排列

    Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...

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

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

  8. hdu 4603 Color the Tree

    这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...

  9. HDU 1055 - Color a Tree

    一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...

随机推荐

  1. [转帖]glib gslibc libc 的关系与区别

    https://blog.csdn.net/Com_ma/article/details/78692092 [glibc 和 libc] glibc 和 libc 都是 Linux 下的 C 函数库. ...

  2. (5.3)mysql高可用系列——mysql复制之复制的参数

    参考:https://www.iteye.com/blog/shift-alt-ctrl-2269539 详情 [1]参数 #[1.1]基本参数 bind-address=192.168.1.201 ...

  3. WSL升级到wsl2并使用strace

    安装wsl的方法,网上很多,一般都ok.这里主要提几个问题: 1.配置securecrt来支持ssh登录. 2.使用strace. 3.使用docker. 由于wsl不支持strace,一直盼着能升级 ...

  4. 坦克大战--Java类型 ---- (1)音乐播放

    实现原理 我用接口java.applet.AudioClip实现音乐播放,那么我们需要了解这个接口的情况. 我们主要使用其中的三个方法: (1)void loop(); //循环播放(2)void p ...

  5. Java回调实现异步

    在正常的业务中使用同步线程,如果服务器每处理一个请求,就创建一个线程的话,会对服务器的资源造成浪费.因为这些线程可能会浪费时间在等待网络传输,等待数据库连接等其他事情上,真正处理业务逻辑的时间很短很短 ...

  6. eclipse+maven搭建springboot项目入门

    一.下载jdk,例如(jdk1.8.171) 安装(注意仅仅安装jdk就可以了,不要安装jre,设置JAVA_HOME,配置jdk环境变量) 二.下载maven(apache-maven-3.5.3- ...

  7. 虚拟机无法启动,提示:无法打开内核功能扩展“com.vmware.kext.vmci”: 无此文件或目录

    打开 系统偏好设置->安全性与隐私->允许打开 即可

  8. ubuntu 安装vim报错

    问题:ubuntu18.04默认没有安装vim,使用 sudo apt install 提示 错误信息: 下列信息可能会对解决问题有所帮助: 下列软件包有未满足的依赖关系: vim : 依赖: vim ...

  9. 08 Json结构化Datetime时间以及保留中文

    错误描述: import json import datetime a = datetime.datetime.now() print(a) b = json.dumps(a) print(b) 如上 ...

  10. mybatis的if

    <select id="findList" resultType="BndExport"> SELECT <include refid=&qu ...