poj 2054 Color a Tree(贪婪)
# 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(贪婪)的更多相关文章
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...
- 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 ...
- POJ 2054 Color a Tree#贪心(难,好题)
题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...
- POJ 2054 Color a Tree (贪心)
$ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- poj2054 Color a Tree
神题.这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价. 那么我们就能想出一个错误的贪 ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
随机推荐
- myeclipse部署时An internal error occurred 错误的几种情况
myecplise上将工程部署到应用下时,经常出现 An internal error occurred during: "Add Deployment". java.lang.N ...
- DDFT
得知DIP文章4日 傅立叶变换数学原理将可能完全被引入,下仅仅实现代码.观察下结果,公式在上一篇博客中已经描写叙述 上代码: // // main.c // Fourer2D // // Create ...
- opencv kmeans 图像分割
利用kmeans算法,将彩色图像的像素点作为样本,rgb值作为样本的属性, 对图像所有的像素点进行分类,从而实现对图像中目标的分割. c++代码(openCV 2.4.11) Scalar color ...
- BZOJ 1053 反素数ant
初读这道题,一定有许多疑惑,其中最大的疑惑便是"反素数",反素数的概念很简单,就是,a<b同时a的因数个数大于b的因数个数.但是想要完成本题还需要一些信息,关于 ...
- pay包注释(二)
@login_required()def to_register(request): return render_to_response("pay/register_yeepay.ht ...
- 使用Powershell 脚本发送邮件乱码问题?
最近在使用Powershell 编码的时候发现一个问题,只要邮件中有中文字符的邮件执行脚本以后,我们发现收到邮件都是乱码,状况如下: 对比下Powershell脚本,我们将邮件的·ENCODING 加 ...
- cocos2dx 3.0 飞机大战
因为课程须要.然后又水平有限.所以写了个飞机大战.加上不会画画.所以图片资源也是从微信apk解压出来的,设计思路參考的偶尔e网事. 闲话不说.先讲一下设计.大体上一共分为3个场景.场景以下是Layer ...
- 七、Nginx学习笔记七Nginx的Web缓存服务
user www; worker_processes 1; error_log /usr/local/nginx/logs/error.log crit; pid /usr/local/nginx/l ...
- ls命令显示可执行的文件 ls -F
ls命令显示可执行的文件 ls -F
- 清华集训2014 day1 task2 主旋律
题目 这可算是一道非常好的关于容斥原理的题了. 算法 好吧,这题我毫无思路,直接给正解. 首先,问题的正面不容易求,那么就求反面吧: 有多少种添加边的方案,使得这个图是DAG图(这里及以下所说的DAG ...