题目链接: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. Maven设置阿里仓库镜像增加访问速度

    修改maven的setting.xml 在mirrors节点下面添加子节点 <mirror> <id>nexus-aliyun</id> <mirrorOf& ...

  2. .Net Core 中使用NLog作为日志中间件

    ⒈安装相关依赖 NLog NLog.Web.AspNetCore ⒉在项目的根目录中创建NLog配置文件 <?xml version="1.0" encoding=" ...

  3. gson 带泛型的转换

    json转对象 public static <T> T json2Obj(String json, Class<T> cls) { Gson gson = new Gson() ...

  4. Css常用的技巧

    一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.  具体内容请浏览:CSS常用缩写语法 二.明确定义单位,除非值为0. 忘记定义尺寸的单位是CSS新手普遍的错误.在HTML中 ...

  5. C# 实体之间转换

    记录一下方便自己下次使用 public class ConvertHelper { public static List<T2> ConvertToList<T1, T2>(L ...

  6. O024、Nova组件如何协同工作

    参考https://www.cnblogs.com/CloudMan6/p/5415836.html   Nova 物理部署方案   前面大家已经看到 Nova 由很多子服务组成,我们也知道OpenS ...

  7. git取消操作命令

    1,移除git add . 的内容 git reset HEAD 2,移除git commit 的内容(commit_A是文件名) git rebase -i commit_A

  8. window.prompt()和 window.confirm()选择

    代码截图: 效果:  代码截图: 效果:

  9. 【项目构建工具】 Gradle笔记1

    一.Gradle简介 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XM ...

  10. MySQL快速清空表数据

    truncate table 可以不删除表的情况下,快速情况表数据