# include <stdio.h>
# include <algorithm>
# include <string.h>
using namespace std;
int father[1010];
int next[1010];//当前集合的下个元素(包含i)
int pre[1010];//当前集合的上个元素(包含i)
int num[1010];//num[i]当前集合储存的点的个数(包含i)
int vis[1010];
int sum[1010];//当前集合的元素和
int c[1010];//点的花费
int n,r;
int find_max()//找到当前权值最大的集合
{
double max=0;
int bh=-1;
for(int i=1;i<=n;i++)
{
if(max<(sum[i]*1.0)/num[i]&&!vis[i])
{
max=(sum[i]*1.0)/num[i];
bh=i;
}
}
return bh;
}
void uni(int x)//联合
{
int i;
for(i=father[x];pre[i]!=-1;i=pre[i])//找到父元素所在的集合
{}
sum[i]+=sum[x];
num[i]+=num[x];
for(i=father[x];next[i]!=-1;i=next[i])//找到父元素所在集合的底元素
{}
next[i]=x;
pre[x]=i;
vis[x]=1; }
int main()
{
int i;
while(~scanf("%d%d",&n,&r),n+r)
{
for(i=1;i<=n;i++)
{
scanf("%d",&c[i]);
vis[i]=0;
sum[i]=c[i];
pre[i]=next[i]=-1;
num[i]=1;
}
for(i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
father[b]=a;
}
int d;
vis[r]=1;//初始点
while(1)
{
d=find_max();
if(d==-1)
break;
uni(d);
}
int ans=0,t=1;
for(i=r;i!=-1;i=next[i])
{
ans+=c[i]*t;
t++;
}
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 2054 Color a Tree(贪婪)的更多相关文章

  1. POJ 2054 Color a Tree

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

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

  3. POJ 2054 Color a Tree#贪心(难,好题)

    题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...

  4. POJ 2054 Color a Tree (贪心)

    $ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...

  5. Color a Tree[HDU1055]

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

  6. POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)

    POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意:  圣诞树是由n个节点和e个边构成的,点编号1-n. ...

  7. poj2054 Color a Tree

    神题.这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价. 那么我们就能想出一个错误的贪 ...

  8. Color a Tree HDU - 6241

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

  9. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

随机推荐

  1. AseoZdpAseo.init(this, AseoZdpAseo.INSERT_TYPE);

    让以后的人知道吧,这就是一个广告包,相当于广告插件.

  2. 定制一个winCE5.0操作系统

    定制一个winCE5.0操作系统 2009-04-01 09:01:14|  分类: winCE|字号 订阅     定制一个操作系统并模拟器上运行,需要以下几个步骤: STEP 1:用Platfor ...

  3. BZOJ 1018

    program bzoj1018; type node=..] of boolean; pair=..] of boolean; var tot,c,i,j,k,x1,y1,x2,y2:longint ...

  4. poj 2245 Lotto(dfs)

    题目链接:http://poj.org/problem?id=2245 思路分析:无重复元素组合组合问题,使用暴力枚举法,注意剪枝条件. 代码如下: #include <iostream> ...

  5. Distinguishing Between Embedded and General-Purpose Computing

    标题:嵌入式系统与通用计算机系统的区别 To understand what falls into the category of embedded computing ,it is instruct ...

  6. cocos2d-html5 简易 下拉表单 控件

    刚才在CH5的群里问了问  有没有大侠写过 下拉表单控件啊!  没人鸟窝 ,DZ老师表示非常伤心啊  ,于是乎  自己写一个把 共享给大家. 效果图上一个  仅仅实现了一个最最主要的控件  非常eas ...

  7. js 验证手机号 以及身份证正则表达式

    手机号:① /^1(3[0-9]{9}|5[0-35-9][0-9]{8}|7[07][0-9]{8}|8[25-9][0-9]{8})$/ ② /^1(3\d|5[0-35-9]|7[07]|8[2 ...

  8. ajaxfileupload.js 文件上传

    一,前台代码. <input id="fileToUpload" type="file" size="25" name="f ...

  9. JAVA 中URL链接中文参数乱码的若干处理方法

    方法一: http://xxx.do?ptname='我是中国人' String strPtname = request.getParameter("ptname"); strPt ...

  10. CentOS6.5实现rsync+inotify实时同步

    参考博文: 参考1:CentOS6.5实现rsync+inotify实时同步 参考2:inotify-tools+rsync实时同步文件安装和配置 CentOS 6.3下rsync服务器的安装与配置  ...