PKU2054 Color a Tree

本文介绍了一种针对树形结构的贪心算法问题,旨在寻找一种染色顺序以使总代价最小。通过分析结点的特性,提出了一种有效的算法实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 CiFi.
Figure-1
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 N1 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.

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是这个树的根。现在要对这N个结点依次进行染色,每个结点染色要花费1个单位的时候,同时要满足一个结点仅在其父亲被染色后才可被染色,每个结点有个权值Ci,如果我们在第Ti时间对i号结点染色,则付出总代价为TiCi (1<=i<=N)
现在给出这个树和每个点的权值,请构造一种染色顺序,使得总代价最小。

思路
如果不是看了题解,我肯定不知道它是贪心。
题解:

在可能的情况下,最大权值的结点,要立刻被染色。这对于解题有什么意义呢,有了上述定理,那么我们可以确定在答案序列中,必然存在PaMax,Max这样的子序列,Max必然紧接着PaMax在序列种出现。不由的有一种想法,能不能将PaMaxMax这两个结点进行合并呢?如何合并呢?
我们这样来思考,在染色了PaMax之后,就要马上进行Max结点染色。合并之后成为一个Union结点,他的权值是二者的和,而将Union节点染色的时间为2。同时MaxPaMax的所有儿子均为Union的儿子,这样就根据题意完成了合并PamaxMax两个结点的工作。由于合并产生了染色需要时间的关系,下面对模型进行扩充:每个结点有一个权值Ci,和一个染色所需要的时间Si,而且每个结点还有一个生成序列,表示依次是哪些结点合并而成。所以我们的贪心思想也有所变化,尽量让Ci/Si比值最大的节点先染色。这很好理解,如果将合并了的结点拆开来看,事实上可以认为是Si个权值为Ci/Si的结点。根据合并的方式,我们得到了一个算法:
1. 令所有节点S值均为1,每个节点生成序列中仅有一个元素,即为它本身。
2. 若树中只剩一个结点,则输出这个这个结点的生成序列。
3. 取出Ci/Si值最大的非根结点Max
4. 将Max和其父亲合并,新合并出的结点Union的各个参数为:Cunion=CMax+CPamaxSUnion=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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值