# 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. HDU2084:数塔(DP)

    Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大 ...

  2. 用C语言写一个程序,得出当前系统的整形数字长(16位,32位,64位)等,不能使用sizeof()

    #include <iostream>#include <cmath>using namespace std; int main(){ int num = -1; unsign ...

  3. Java学习之IO之File类二

    之前学了File便想把我学习视频的名字改了,因为文件名太长不好看,便试着写了个功能实现 package com.gh.file; import java.io.File; /** * 批量文件命名 * ...

  4. poj 1731 Orders(暴力)

    题目链接:http://poj.org/problem?id=1731 思路分析:含有重复元素的全排列问题:元素个数为200个,采用暴力枚举法. 代码如下: #include <iostream ...

  5. 简单的iOS抽屉效果

    #define screenW [UIScreen mainScreen].bounds.size.width #import "ViewController.h" @interf ...

  6. SQL Server SQL高级查询语句小结(转)

    --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select disti ...

  7. delphi DCC32命令行方式编译delphi工程源码

    本文链接地址:http://blog.csdn.net/sushengmiyan/article/details/10284879 作者:苏生米沿 Borland出品的Delphi,在TIOBE公布的 ...

  8. JS判断只能是数字和小数点

    JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'' ...

  9. [ArcGIS必打补丁]ArcGIS 10.1 SP1 for (Desktop, Engine, Server) Quality Improvement Patch

    大家都知道假设希望保证企业级GIS系统的稳定执行,除了使用最新的ArcGIS版本号产品以外,还须要打上相关的补丁. 补丁分为:Service Pack和Patch 比如,假设你使用的ArcGIS10. ...

  10. 【论文阅读】Parsing Clothing in Fashion Photographs(翻译与理解)

    发表于2012年 作者:Kota Yamaguchi M.Hadi Kiapour Luis E.Ortiz Tamara L.Berg 摘要:展示了一个从时装图片中解析衣服的有效方法,提供了一个一般 ...