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”,
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
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 1
1 2 1 2 4
1 2
1 3
2 4
3 5
0 0
Sample Output
33
Source
Beijing 2004
题目翻译
有一个N个结点的有根树,1是这个树的根。现在要对这
现在给出这个树和每个点的权值,请构造一种染色顺序,使得总代价最小。
思路
如果不是看了题解,我肯定不知道它是贪心。
题解:
在可能的情况下,最大权值的结点,要立刻被染色。这对于解题有什么意义呢,有了上述定理,那么我们可以确定在答案序列中,必然存在PaMax,Max这样的子序列,Max必然紧接着PaMax在序列种出现。不由的有一种想法,能不能将PaMax和Max这两个结点进行合并呢?如何合并呢?
我们这样来思考,在染色了PaMax之后,就要马上进行Max结点染色。合并之后成为一个Union结点,他的权值是二者的和,而将Union节点染色的时间为2。同时Max和PaMax的所有儿子均为Union的儿子,这样就根据题意完成了合并Pamax和Max两个结点的工作。由于合并产生了染色需要时间的关系,下面对模型进行扩充:每个结点有一个权值Ci,和一个染色所需要的时间Si,而且每个结点还有一个生成序列,表示依次是哪些结点合并而成。所以我们的贪心思想也有所变化,尽量让Ci/Si比值最大的节点先染色。这很好理解,如果将合并了的结点拆开来看,事实上可以认为是Si个权值为Ci/Si的结点。根据合并的方式,我们得到了一个算法:
1. 令所有节点S值均为1,每个节点生成序列中仅有一个元素,即为它本身。
2. 若树中只剩一个结点,则输出这个这个结点的生成序列。
3. 取出Ci/Si 值最大的非根结点Max。
4. 将Max和其父亲合并,新合并出的结点Union的各个参数为:Cunion=CMax+CPamax,SUnion=SMax+SPaMax,同时Union的生成序列为PaMax的生成序列与Max的生成序列连接而成。
5. 重复2~4步。
我们来分析一下整个算法的时间复杂度,维护所有结点集合,快速找出权值最大的非根结点可以使用二叉堆。而对结点进行和并可以使用并查集。其中维护堆总时间复杂度为O(Nlog2N),而并查集时间复杂度为O(N∗α(n)),所以总时间复杂度为O(Nlog2N)。
不会O(Nlog2N)写法啊。就写个O(n2)好了。找权值最大的非根节点?暴力!对节点合并?暴力!反正O(n2)写法也可以过啦。
代码
#include <cstdio>
#include <queue>
#include <cstring>
const int maxn=100000;
int fa[maxn+10],time[maxn+10],val[maxn+10],n,root,ans;
int solve()
{
scanf("%d%d",&n,&root);
if((n==0)&&(root==0))
{
return 1;
}
for(int i=1; i<=n; i++)
{
scanf("%d",&val[i]);
ans+=val[i];
time[i]=1;
}
for(int i=1; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
fa[b]=a;
}
for(int i=1; i<n; i++)
{
double maxnum=0;
int maxi;
for(int i=1; i<=n; i++)
{
if(((double)val[i]/time[i]>maxnum)&&(i!=root))
{
maxnum=(double)val[i]/time[i];
maxi=i;
}
}
for(int i=1; i<=n; i++)
{
if(fa[i]==maxi)
{
fa[i]=fa[maxi];
}
}
ans+=time[fa[maxi]]*val[maxi];
time[fa[maxi]]+=time[maxi];
val[fa[maxi]]+=val[maxi];
val[maxi]=0;
}
printf("%d\n",ans);
return 0;
}
int main()
{
while(1)
{
memset(fa,0,sizeof fa);
memset(time,0,sizeof time);
memset(val,0,sizeof val);
ans=0;
root=0;
n=0;
if(solve())
{
return 0;
}
}
}