题目链接: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. mysql大数据量插入参考

    Mysql 千万数据10秒批量插入只需三步第一步:配置my.ini文件文件中配置bulk_insert_buffer_size=120M 或者更大将insert语句的长度设为最大.Max_allowe ...

  2. MySQL中关于主从数据库同步延迟的问题解决

    MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...

  3. [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)

    [BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...

  4. mysql行(记录)的详细的操作

    目录 一 介绍 二 插入(增加)数据INSERT 三 更新(修改)数据UPDATE 四 删除数据DELETE 五 查询数据SELECT(重点) 六 权限管理 阅读目录 一 介绍 MySQL数据操作: ...

  5. 接口测试-免费开放的api

    归纳一些不错的免费开放的api 1.Apizza免费开放的Api接口 链接: https://www.jianshu.com/p/e6f072839282 接口文档:https://www.apiop ...

  6. DIj

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace DefineG ...

  7. C# 枚举转集合

    记录一下,方便自己下次使用. public class EnumHelper { /// <summary> /// 将枚举转为集合 /// </summary> /// &l ...

  8. JS根据一个经纬度及距离角度,算出另外一个经纬度

    var mapNumberUtil = {}; /** * 根据一个经纬度及距离角度,算出另外一个经纬度 * @param {*} lng 经度 113.3960698 * @param {*} la ...

  9. Django的MySQL Driver配置

    PEP 249规定了Python的数据库API.MySQL主要有三种API实现: MySQLdb 是Andy Dustman开发的.使用原生代码/C语言绑定的驱动,它已经开发了数十年. mysqlcl ...

  10. idea自动抽取变量快捷键设置

    file---setting---keymap---搜索variable 如下图:默认是ctrl+alt+v,这里修改成自己比较方便的快捷键即可,我这里设置的是alt+e