HDU 6060 RXD and dividing

来源:互联网 发布:mini mac恢复出厂设置 编辑:程序博客网 时间:2024/05/16 18:14

RXD and dividing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 441    Accepted Submission(s): 179


Problem Description
RXD has a tree T, with the size of n. Each edge has a cost.
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T
he wants to divide 2,3,4,5,6,n into k parts S1,S2,S3,Sk,
where Si={2,3,,n} and for all different i,j , we can conclude that SiSj=
Then he calulates res=ki=1f({1}Si).
He wants to maximize the res.
1kn106
the cost of each edge[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost 
 

Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.
The next n1 lines consists of 2 integers, a,b,c, means a tree edge (a,b) with cost c.
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n100.
 

Output
For each test case, output an integer, which means the answer.
 

Sample Input
5 41 2 32 3 42 4 52 5 6
 

Sample Output
27
 

Source
2017 Multi-University Training Contest - Team 3

题意:给出一棵有n个顶点的树,然后将2~n号顶点分成k块,求1号顶点到分成k块后各个顶点的最大权值和。
分析:这里提到了最小斯坦纳树,比赛时还纠结了半天,后来发现这里的树本来就是一棵最小生成树了,然后最小生成树又是斯坦纳树的一种特殊情况,所以这里可以忽略斯坦纳树的情况。而我们需要考虑的是某些边的重用情况。如果将某些子结点分成与父亲结点不同的块的话,那么到达这些子结点就需要重用到达父亲结点的边,也就是相应权值。以此类推~至于多个结点可以分成多个块,由于这棵树已经是最小生成树了,那么在某些块的路径上插入某些结点,并入这个块,也不会对结果有什么影响。

下面是官方题解:

把1看成整棵树的根. 问题相当于把2\sim n2n每个点一个[1, k][1,k]的标号. 然后根据最小斯坦纳树的定义, (x, fa_x)(x,fax) 这条边的贡献是 x 子树内不同标号的

个数目dif_idifi. 那么显然有dif_i\leq min(k, sz_i)difimin(k,szi)sz_iszi表示子树大小. 可以通过构造让所有dif_idifi都取到最大值. 所以答案就是\sum_{x = 2}^{n}{w[x][fa_x] * min(sz_x, k)}

x=2nw[x][fax]min(szx,k) 时间复杂度O(n)O(n)


代码:

#include<bits/stdc++.h>using namespace std;struct Edge{    int v,w;};Edge temp;vector<Edge> vec[1000005];int Size[1000005];int w[1000005];void dfs(int u,int pre){    Size[u]=1;    int len=vec[u].size();    for(int i=0;i<len;i++){        int v=vec[u][i].v;        if(v!=pre){            w[v]=vec[u][i].w;            dfs(v,u);            Size[u]+=Size[v];        }    }}int main(){    int n,k;    while(~scanf("%d%d",&n,&k)){        for(int i=1;i<=n;i++){            vec[i].clear();            Size[i]=0;            w[i]=0;        }        for(int i=1;i<=n-1;i++){            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            temp.v=v;            temp.w=w;            vec[u].push_back(temp);            temp.v=u;            vec[v].push_back(temp);        }        dfs(1,-1);        long long sum=0;        for(int i=2;i<=n;i++){            sum+=(long long)w[i]*min(Size[i],k);        }        printf("%lld\n",sum);    }    return 0;}



原创粉丝点击