hdu 6060 RXD and dividing(dfs)

原创 2017年08月04日 10:05:59

RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 65 Accepted Submission(s): 15
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 Si⋂Sj=∅.
Then he calulates res=∑ki=1f({1}⋃Si).
He wants to maximize the res.
1≤k≤n≤106
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 n−1 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 n≤100.

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

题意:
将一棵树里面的点分成k个集合,并在集合里加入一些边使的一个集合里的任意两个点连通,求使得所有加入集合的边的和的最大值

解析:
要是边的和最大,所以要尽可能多地使用这些边。
所以一颗子树里的孩子节点都要尽可能分到不同的集合里。如果k足够大,那么每一棵子树的孩子节点(包括孙子节点)+子树根节点都可以分到不同的集合,那么这棵子树的根节点向上连接的边(就是该节点与其父节点连接的边)被计算了(child+1)次。但k不足够大即(child+1>k),那么子树的孩子节点最多只能被分到k个集合里,所以根节点向上连接的边只被计算了k次,因此边 的计算次数就是min(child+1,k)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 1000010
#define INF 1999999999
using namespace std;
typedef long long ll;

typedef struct node
{
    ll next;
    ll val;
}node;

ll n,k;
vector<node> edge[MAXN];
ll ans;

ll dfs(ll a,ll valu)  //用dfs来算一颗树的所有孩子节点+孙子节点
{
    if(!edge[a].size())
    {
        ans=ans+valu*(0+1);
        return 1;
    }
    ll child=0;
    for(int i=0;i<edge[a].size();i++)
    {
        child+=dfs(edge[a][i].next,edge[a][i].val);
    }
    ll ch=min(child+1,k);    
    ans+=(ch)*valu;

    return child+1;


}

int main()
{
    ll a;
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        for(int i=1;i<=n;i++)edge[i].clear();
        for(ll i=0;i<n-1;i++)
        {
            node tmp;
            scanf("%lld%lld%lld",&a,&tmp.next,&tmp.val);
            edge[a].push_back(tmp);
        }
        ans=0;

        dfs(1,0);
        printf("%lld\n",ans);
    }
    return 0;

}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/76662794

HDU:6060 RXD and dividing (DFS)

RXD has a tree TT, with the size of nn. Each edge has a cost. Define f(S)f(S) as the the cost of the...
  • hl_1997
  • hl_1997
  • 2018-04-19 21:46:59
  • 6

【HDU 6060 RXD and dividing】+ DFS

RXD and dividingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others...
  • WYK1823376647
  • WYK1823376647
  • 2017-08-02 10:42:48
  • 216

HDU 6060 RXD and dividing【DFS】

RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other...
  • Puyar_
  • Puyar_
  • 2017-08-03 09:32:46
  • 100

hdu 6060 RXD and dividing (dfs)

怎么说呢,比赛的时候没看懂题,对就是没看懂题,应该多读几遍题,不要浮躁,浮躁解决不了问题,求得是最大的res,f(s)是最小的,就是求1-s的最短路径吧,说的应该是唯一的那条路径,不要走1-2-1-2...
  • clx55555
  • clx55555
  • 2017-08-08 09:26:03
  • 78

Hdu 6060 - RXD and dividing (dfs)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6060题目大意: 一颗以结点11为根的树,对2−n2-n的结点作一个划分,但至多使用k个集合,对每...
  • SD_Stjean
  • SD_Stjean
  • 2017-08-04 16:00:22
  • 103

HDU 6060 RXD and dividing

2017多校3-5 RXD and dividing思维题,dfs题意给一棵树,1为根。将2~n划分成不同的集合,并将1加入每个集合。使得每个集合构成的最小斯坦纳树的和最大。思路把1看成整棵树的根. ...
  • xzxxzx401
  • xzxxzx401
  • 2017-08-01 19:27:27
  • 323

HDU 6060 RXD and dividing

题目链接题目意思给一棵树T,有n个结点。 给一个k,表示有k个集合,我们需要把2,3,4,…n号节点放入集合,要保证k个集合的并集等于{2,3,4,5n},并且集合互不相交。(集合可以为空) 然后...
  • qq_37412229
  • qq_37412229
  • 2017-08-08 11:00:56
  • 96

RXD and dividing(HDU 6060)

RXD has a tree T, with the size of n. Each edge has a cost. Define f(S) as the the cost of the minim...
  • luyehao1
  • luyehao1
  • 2017-08-03 10:00:44
  • 114

[HDU]-6060 RXD and dividing

RXD has a tree T, with the size of n. Each edge has a cost. Define f(S) as the the cost of the mini...
  • ctsas
  • ctsas
  • 2017-08-01 20:54:57
  • 147

hdu 6060 RXD and dividing

链接题意:有一颗n个节点的树,现在将节点2-n分成k组,定义每组的的权值为该组内所有点加编号为1的节点相互连接所经过的边的权值的和,求k组点集最大的和。分析:使用贪心的思想,我们思考每条边对最终答案的...
  • your_eyes_see_sea
  • your_eyes_see_sea
  • 2017-08-02 15:38:12
  • 391
收藏助手
不良信息举报
您举报文章:hdu 6060 RXD and dividing(dfs)
举报原因:
原因补充:

(最多只允许输入30个字)