POJ 2054 Color a Tree
贪心。。。。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6647 | Accepted: 2249 |
Description
Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, ..., N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try.
Each node has a "coloring cost factor", Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci * Fi.
For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33.
Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes.
Input
A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.
Output
Sample Input
5 1
1 2 1 2 4
1 2
1 3
2 4
3 5
0 0
Sample Output
33
Source
贪心原则应该是Ci大的尽量先染色,但是由于父节点染了才能染子节点的限制使得问题不好解决了,但是Ci大的一定是在其父节点染色后立即被染色,这时大牛们的思路我也没有看明白如何证明的,但仔细一想就明白了。于是我们根据这个条件就可以将Ci大的点与其父节点合并在一起组成一个集合。这样就可以将问题规模减小。
合并后的点(即集合)的属性如何变化呢?假如设fact[i]表示集合的Ci和,iNum[i]表示i所属集合的结点个数;那么把fact[i]/iNum[i]作为贪心原则,其值大者先合并到其父节点,最终合并成一个集合。
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; struct Edge
{
int to,next;
}e[]; int n,root,Size,Adj[],c[],num[],father[];
bool vis[]; void Init()
{
Size=;
memset(Adj,-,sizeof(Adj));
memset(vis,false,sizeof(vis));
} void Add_Edge(int u,int v)
{
///u-->v
e[Size].to=v;
e[Size].next=Adj[u];
Adj[u]=Size++;
} int Find()
{
int k=-;
double maxn=-0x3f3f3f3f;
for(int i=;i<=n;i++)
{
if(!vis[i]&&i!=root&&maxn<(double)c[i]/num[i])
{
maxn=(double)c[i]/num[i];
k=i;
}
}
return k;
} void Union(int a,int b)
{
/// a to b
num[b]+=num[a];
c[b]+=c[a];
father[a]=b;
for(int i=Adj[a];~i;i=e[i].next)
{
int v=e[i].to;
father[v]=b;
}
} int solve()
{
int ans=;
for(int i=;i<n-;i++)
{
int k=Find();
vis[k]=true;
int p=father[k];
while(vis[p]) p=father[p];
ans+=c[k]*num[p];
Union(k,p);
}
ans+=c[root];
return ans;
} int main()
{
while(scanf("%d%d",&n,&root)!=EOF)
{
if(n==&&root==) break;
Init();
for(int i=;i<=n;i++)
{
scanf("%d",c+i);
num[i]=;
}
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
Add_Edge(u,v);
father[v]=u;
}
printf("%d\n",solve());
}
return ;
}
POJ 2054 Color a Tree的更多相关文章
- 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(贪婪)
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- 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 ...
随机推荐
- Android开发环境搭建中的一些小问题记录
1.由于市场上大多数教程是基于Eclipse,而AndroidStudio显然是大势所趋,所有我在电脑上同时搭建了两个IDE,直接在官网下载AndroidStudio比较好,因为SDK,AVD都集成了 ...
- 看看你的正则行不行——正则优化一般的json字符串
json字符串很有用,有时候一些后台接口返回的信息是字符串格式的,可读性很差,这个时候要是有个可以格式化并高亮显示json串的方法那就好多了,下面看看一个正则表达式完成的json字符串的格式化与高亮显 ...
- Linux Command Line Basics
Most of this note comes from the Beginning the Linux Command Line, Second Edition by Sander van Vugt ...
- AngularJs ngCloak、ngController、ngInit、ngModel
ngCloak ngCloak指令是为了防止Angular应用在启动加载的时候html模板将会被短暂性的展示.这个指令可以用来避免由HTML模板显示造成不良的闪烁效果. 格式: ng-cloak ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...
- 网络存储(二)之ISCSI原理
组成 一个简单ISCSI系统大致由以下部分构成 ISCSI Initiator 或者 ISCSI HBA ISCSI Target 以太网交换机 一台或者多台服务器 结构图如下: iscsi服务器用来 ...
- IOS 中的CoreImage框架(framework)
http://www.cnblogs.com/try2do-neo/p/3601546.html coreimage framework 组成 apple 已经帮我们把image的处理分类好,来看看它 ...
- 未签名有元程序集 Unsigned Friend Assemblies
C#中的访问修饰符internal可以使类型在同程序集中可以被相互访问.但有时会有这样一个要求,我们希望一个程序集中的类型可以被外部的某些程序集访问,如果设置成public的话,就被所有的外部程序集访 ...
- properties 配置文件中值换行的问题
在使用properties配置文件的时候我们经常碰到如下两个问题 1:当a=b中的b值内容特别长的时候为了阅读方便我们手动换行,但如果我们直接回车那么后面的数据就会丢失.那如何解决呢? 例如: a=a ...
- scrapy3_ 安装指南
安装指南 安装Scrapy 注解 请先阅读 平台安装指南. 下列的安装步骤假定您已经安装好下列程序: Python 2.7 Python Package: pip and setuptools. 现在 ...