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

找出当前最大子节点和其父节点合并成新的节点
直到点集中只剩一个节点
因为合并时将子节点的权值加在父节点上
则 每次更新 答案加上当前子节点的权值乘上父节点的花费时间,这个很神奇
因为合并后,当对其父节点操作时也会重复加上子节点权值,
即累加起来等同于每一个节点的最初权值乘上对应时间

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct node{
int f,v,t;
double w;//权值
}p[];
int n,r,a,b;
int main()
{
while(~scanf("%d%d",&n,&r),n+r)
{
int ans=;
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v);
p[i].w=p[i].v;
p[i].t=;
ans+=p[i].v;
}
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
p[b].f=a;
}
double wmax;
int pos,fa;
for(int i=;i<n;i++)
{
wmax=-;
for(int j=;j<=n;j++)//找当前大子节点
{
if(j==r) continue;//子节点!
if(wmax<p[j].w)//点权值比较时对比算数平均值
{
wmax=p[j].w;
pos=j;
}
}
p[pos].w=-;//去掉子节点
fa=p[pos].f;
ans+=p[pos].v*p[fa].t;//更新答案
for(int j=;j<=n;j++)//将子节点的子节点全部接在新建节点上
if(p[j].f==pos) p[j].f=fa;
p[fa].v+=p[pos].v;//新节点在父节点位置,节点的总权值等同于父子权值和
p[fa].t+=p[pos].t;//个数
p[fa].w=1.0*p[fa].v/p[fa].t;//比较时权值等于算术平均值
}
printf("%d\n",ans);
}
}

HDU 1055 - Color a Tree的更多相关文章

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

  2. hdu 4603 Color the Tree

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

  3. Color a Tree HDU - 6241

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

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. POJ 2054 Color a Tree

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

  6. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Color a Tree[HDU1055]

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

  9. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. ibatis复用SQL片段、引入片段 动态条件增加

    1:ibatis复用SQL片段.引入片段  使用[sql]和[include]标签: 通常情况下,你会这样写:xml 代码 <select id="selectItemCount&qu ...

  2. IIS Hang Troubleshoot

    Your website maybe stop working and response very lowly. How to find out the reason? Below are the g ...

  3. BlockingQueue

    BlockingQueue的使用 http://www.cnblogs.com/liuling/p/2013-8-20-01.html BlockingQueue深入分析 http://blog.cs ...

  4. UVA11401Triangle Counting(简单计数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目意思:从1,2,3,……,n中选出3个不同的整数使其构成一个三角形,有多少种方 ...

  5. TextView之一:子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...

  6. java 数字补齐0

    String str_f = str.substring(0, 1); int i = (Integer.parseInt(str.substring(1)) + 1); // 数字补齐0 Decim ...

  7. python运维开发(二十二)---JSONP、瀑布流、组合搜索、多级评论、tornado框架简介

    内容目录: JSONP应用 瀑布流布局 组合搜索 多级评论 tornado框架简介 JSONP应用 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. ...

  8. Window 点击“X”关闭之后无法show

    将Window的属性closeAction设置为hide就可以了. var panel1 = Ext.create('Ext.panel.Panel', { title: 'this is panel ...

  9. WinForm 鼠标进入移开窗体事件,因子控件导致的误触发

    /// <summary> /// 重写OnControlAdded方法,为每个子控件添加MouseLeave事件 /// </summary> /// <param n ...

  10. javascript之Number

    一.构造函数 Number(value) new Number(value) 二.Number属性 1.Number.MAX_VALUE 返回能表示的最大数字. 2.Number.MIN_VALUE ...