【多校训练】hdu 6060 RXD and dividing dfs

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 4 1 2 3 2 3 4 2 4 5 2 5 6
 

Sample Output
  
  
27
 

题意:

有一颗n个节点的树,现在将节点2-n分成k组,定义每组的的权值为该组内所有点加编号为1的节点相互连接所经过的边的权值的和,求k组点集最大的和。

思路:

把1看成整棵树的根. 问题相当于把2∼n2n每个点一个[1,k][1,k]的标号. 然后根据最小斯坦纳树的定义, (x,fax)(x,fax) 这条边的贡献是 x 子树内不同标号的个数目difidifi. 那么显然有difi≤min(k,szi)difimin(k,szi)sziszi表示子树大小. 可以通过构造让所有difidifi都取到最大值. 所以答案就是∑x=2nw[x][fax]∗min(szx,k)x=2nw[x][fax]min(szx,k) 时间复杂度O(n)O(n).

//
//  main.cpp
//  1005
//
//  Created by zc on 2017/8/21.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1100000;
const int M=4400000;
int cnt,head[N],n,k;
ll ans;
struct node
{
    int next,v;
    ll c;
}e[M];

void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}

void add(int u,int v,ll c)
{
    e[cnt].c=c;
    e[cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

int dfs(int t,int fa)
{
    int sum=1;
    for(int i=head[t];i+1;i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa)   continue;
        int p=dfs(v,t);
        sum+=p;
        ans+=min(k,p)*e[i].c;
    }
    return sum;
}

int main(int argc, const char * argv[]) {
    while(~scanf("%d%d",&n,&k))
    {
        init();
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            ll c;
            scanf("%d%d%lld",&u,&v,&c);
            add(u,v,c);
            add(v,u,c);
        }
        ans=0;
        dfs(1,-1);
        printf("%lld\n",ans);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值