HDOJ 题目1055Color a Tree(贪心,并查集)
来源:互联网 发布:mac自带截图快捷键 编辑:程序博客网 时间:2024/06/05 01:00
Color a Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1058 Accepted Submission(s): 343
Problem Description
Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root to each of the other nodes.
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.
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
The input consists of several test cases. The first line of each case contains two integers N and R (1 <= N <= 1000, 1 <= R <= N), where N is the number of nodes in the tree and R is the node number of the root node. The second line contains N integers, the i-th of which is Ci (1 <= Ci <= 500), the coloring cost factor of node i. Each of the next N-1 lines contains two space-separated node numbers V1 and V2, which are the endpoints of an edge in the tree, denoting that V1 is the father node of V2. No edge will be listed twice, and all edges will be listed.
A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.
A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.
Output
For each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes.
Sample Input
5 11 2 1 2 41 21 32 43 50 0
Sample Output
33
Source
Asia 2004, Beijing (Mainland China)
Recommend
We have carefully selected several similar problems for you: 1053 1050 1789 1045 3177
思路:贪心的总体思路是:每次找到一个权值最大的节点,如果它是根节点,则首先对它染色,否则的话我们可以得出一个结论,在对它的父亲已经染色的情况下,立刻给它染色是最优的。现在重点讨论第二种情况,当它不是根节点时,我们如果对它父亲染了色,则一定会立刻对它染色,所以可以把它和它父亲合并为同一个节点,它和它父亲的儿子都成为了新节点的儿子,它的父亲的父亲则是新节点的父亲。
ac代码
#include<stdio.h>#include<string.h>int next[100000],f[100000],ff[100000],cot[100000],v[100000];double a[100000],b[100000];int fc(int x){int r=x;while(r!=next[r]){r=next[r];}return r;}int fun(int x){int r=x;while(r!=ff[r])r=ff[r];return r;}int main(){int n,r;while(scanf("%d%d",&n,&r)!=EOF,n||r){int i,j,k,x,y;double sum=0;memset(v,0,sizeof(v));for(i=1;i<=n;i++){scanf("%lf",&a[i]);f[i]=ff[i]=next[i]=i;cot[i]=1;b[i]=a[i];}for(i=0;i<n-1;i++){int u,v;scanf("%d%d",&u,&v);f[v]=u;}v[r]=1;for(j=1;j<n;j++){double max=0;//int a,b;for(i=1;i<=n;i++){if(!v[i]&&max<a[i]/cot[i]){max=a[i]/cot[i];x=i;}}v[x]=1;y=fc(f[x]);next[y]=x;ff[x]=y;y=fun(x);a[y]+=a[x];cot[y]+=cot[x];}k=1;while(r!=next[r]){sum+=k*b[r];k++;r=next[r];}sum+=k*b[r];printf("%.lf\n",sum);}}
0 0
- HDOJ 题目1055Color a Tree(贪心,并查集)
- 并查集,贪心:Color A Tree
- Pku2054 Color a Tree(并查集优化+堆优化+贪心)
- Pku2054 Color a Tree(并查集优化+堆优化+贪心)
- HDOJ 题目1325 Is It A Tree?(并查集)
- HDOJ 1325 Is It A Tree?(并查集)
- HDOJ 1325 Is It A Tree? (并查集)
- HDOJ 1325 Is It A Tree(并查集)
- hdoj tree 5606 (并查集)
- hdoj 1325 Is It A Tree? 【并查集】
- hdu-1055----树染色问题color a tree(贪心)
- HDU oj 1055(Color a Tree 贪心好题)
- hdu 1055 Color a Tree 贪心
- hdu 1055 Color a Tree 贪心
- HDU 1055:Color a Tree【贪心】
- HDOJ 题目1829A Bug's Life(分组并查集)
- HDOJ 1325 Is It A Tree?(并查集+入度判断)
- 2054 Color a Tree 贪心
- Js获取下拉框选定项的值和文本
- 解惑关于多台服务器并发问题涉及到数据库锁的问题
- AlertDialog.Builder属性集合
- 博客开始整理了
- 翻转句子中单词的顺序
- HDOJ 题目1055Color a Tree(贪心,并查集)
- java-汉字转换拼音工具
- FTP API函数
- HDU 3371 最小生成树 kruskal
- 读v_JULY_v整理笔试题博客有感,整理些答案。
- 在Linux下编写C程序,怎么检查程序是否有内存泄漏?
- django+celery+RabbitMQ 环境配置
- Java中Properties类的使用
- 客户端封装数据为json格式的数据提交到服务端的方法