关闭

RXD and dividing(HDU 6060)

标签: RXD and dividingHDU 6060DFS
104人阅读 评论(0) 收藏 举报
分类:

RXD and dividing

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


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
 

//题意:给定一棵树,树里每条边有一个权值,现在以1为根节点,把2-n划分成k个集合,每个点只能出现在一个集合里且k个集合里包含2-n所有点(即分成k个划分)。求根节点1到各个集合的路径的最大值(经过集合里所有点)。

//思路

官方题解

把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)

解释:我们要求每条边的贡献,贡献怎么求?就是经过这条边的次数*这条边的权值。而经过这条边的次数<=min(k,size),size是子树大小,因为要求最大值,所以次数就=min(k,size)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

//这里MAX太大,VS里跑不起来,会直接报错
//但hdu OJ上能运行起来,可以先开小点调试下,然后改成1e6去交
const int MAX = 1e6 + 10;

typedef struct {
	int val, to;
}Edge;

//sum要long long,不然会WA
long long sum;
int n, k;
vector<Edge>map[MAX];
int Size[MAX];

void dfs(int root, int front)
{
	for (int i = 0; i < map[root].size(); i++)
	{
		//因为是无向边,防止死循环
		if (map[root][i].to == front)
			continue;
		dfs(map[root][i].to, root);
		Size[root] += Size[map[root][i].to];
		//注意这里*个1LL
		sum += 1LL * map[root][i].val*min(Size[map[root][i].to], k);
	}
}

int main()
{
	while (scanf("%d%d", &n, &k) != EOF)
	{
		sum = 0;
		for (int i = 0; i <= n; i++)
		{
			map[i].clear();
			Size[i] = 1;
		}
		for (int i = 0; i < n - 1; i++)
		{
			int a, b, c;
			Edge temp;
			scanf("%d%d%d", &a, &b, &c);
			//这里是无向边,两个点要互相到达
			temp.val = c;
			temp.to = b;
			map[a].push_back(temp);
			temp.to = a;
			map[b].push_back(temp);
		}
		dfs(1,0);
		printf("%lld\n", sum);
	}
	return 0;
}



0
0
查看评论

hdu 6060 RXD and dividing

链接题意:有一颗n个节点的树,现在将节点2-n分成k组,定义每组的的权值为该组内所有点加编号为1的节点相互连接所经过的边的权值的和,求k组点集最大的和。分析:使用贪心的思想,我们思考每条边对最终答案的贡献。既然要结果最大,那么每条边就尽可能的多被走到。 如上图,现在考虑A-C这条边所做的贡献,很...
  • your_eyes_see_sea
  • your_eyes_see_sea
  • 2017-08-02 15:38
  • 376

[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 minimal Steiner Tree of the set S on tree T. he want...
  • ctsas
  • ctsas
  • 2017-08-01 20:54
  • 128

HDU 6060 RXD and dividing

2017多校3-5 RXD and dividing思维题,dfs题意给一棵树,1为根。将2~n划分成不同的集合,并将1加入每个集合。使得每个集合构成的最小斯坦纳树的和最大。思路把1看成整棵树的根. 问题相当于把2∼n2\sim n每个点一个[1, k]的标号. 然后根据最小斯坦纳树的定义, (x,...
  • xzxxzx401
  • xzxxzx401
  • 2017-08-01 19:27
  • 313

HDU 6060 RXD and dividing

题目链接题目意思给一棵树T,有n个结点。 给一个k,表示有k个集合,我们需要把2,3,4,…n号节点放入集合,要保证k个集合的并集等于{2,3,4,5n},并且集合互不相交。(集合可以为空) 然后每次取一个集合Si与{1}求并,得到比如{1,2,3},那么tempi = f({1,2,3});f...
  • qq_37412229
  • qq_37412229
  • 2017-08-08 11:00
  • 92

Hdu 6060 RXD and dividing【思维】

RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 22...
  • mengxiang000000
  • mengxiang000000
  • 2017-08-01 18:59
  • 196

【HDU 6060 RXD and dividing】+ DFS

RXD and dividingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 758 Accepted Submission...
  • WYK1823376647
  • WYK1823376647
  • 2017-08-02 10:42
  • 205

HDU 6060 RXD and dividing【DFS】

RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1344&#...
  • Puyar_
  • Puyar_
  • 2017-08-03 09:32
  • 89

Hdu 6060 - RXD and dividing (dfs)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6060题目大意: 一颗以结点11为根的树,对2−n2-n的结点作一个划分,但至多使用k个集合,对每个分块产生的集合中加入根节点1,然后计算集合内结点互相可达最少需要的权值的和(非最小生成树,可能需要...
  • SD_Stjean
  • SD_Stjean
  • 2017-08-04 16:00
  • 94

hdu 6060 RXD and dividing(dfs)

RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1453 Accepted Submiss...
  • qq_37025443
  • qq_37025443
  • 2017-08-04 10:05
  • 67

HDU 6060 RXD and dividing(思维)

题目:点击打开链接 题意:这道题看了两天才看懂,zz。。。说是什么高大上的Steiner树,其实并不高大。又他本来给的就是一棵树,所以也不是最小生成树。大概意思是有一棵树,顶点从1到n;有n-1条边,每条边有个权值;要把从2到n的n-1个点分到k个集合Si里去,每个集合不能有相同的点,然后f(Si...
  • addkai
  • addkai
  • 2017-08-02 12:42
  • 76
    个人资料
    • 访问:8970次
    • 积分:801
    • 等级:
    • 排名:千里之外
    • 原创:71篇
    • 转载:2篇
    • 译文:0篇
    • 评论:8条
    最新评论