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 ...
随机推荐
- spring 事务 笔记3.1
Spring事务 以前的事务都是编程式事务,需要开启和关闭,然后程序写在这里面 spring,声明式事务 Spring事务隔离级别 DEFAULT 使用数据库默认隔离级别 READ_UNCOMMITT ...
- c++ 复制构造函数和赋值函数
c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...
- cmake 学习笔记(三)
转自:http://blog.csdn.net/dbzhang800/article/details/6329314 接前面的 Cmake学习笔记(一) 与 Cmake学习笔记(二) 继续学习 cma ...
- 大Q品牌故事_大Q官网_腾讯旗下买卖宝公司倾力打造
大Q品牌故事_大Q官网_腾讯旗下买卖宝公司倾力打造 走在大路上的改变者,有态度的互联网手机品牌
- svn恢复到之前某个版本号
一直在找svn回滚的方法,这个还是非常有用的,屡试不爽阿 常常因为坑爹的需求,功能要切回到之前的某一个版本号.有两种方法能够实现: 方法1: 用svn merge 1) 先 svn up,保证更新到 ...
- Eclipse快捷键 10个最有用的快捷键(转载)
现在很多开发人员都在用eclipse.用开发工具,就是为了方便,方便你开发你的软件,方便你管理你的工程,而开发工具提供各种功能大部分会有对应的快捷键,下面就列出了eclipse的快捷键. Ecli ...
- UIApplication相关
1,显示应用图标右上角的红色提示 application.applicationIconBadgeNumber = 10; 2.修改状态栏的类型 在当前控制器下设置 - (UIStatusBarSty ...
- UIAlertView、UIActionSheet兼容iOS8
链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...
- PHP学习笔记3-逻辑运算符
逻辑运算符图解: 逻辑且&&: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/26 ...
- Ural 1001 - Reverse Root
The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...