【HDU 6060 RXD and dividing】+ DFS
来源:互联网 发布:广数螺纹编程格式 编辑:程序博客网 时间:2024/06/14 22:14
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 758 Accepted Submission(s): 319
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
Source
2017 Multi-University Training Contest - Team 3
题意 : 给出一棵树,把除 1 以外的节点分成 k 个集合,s(i) 等于集合 i 中所有的点联通的边权
题解 : 把1看成整棵树的根. 问题相当于把2\sim n2∼n每个点一个[1, k][1,k]的标号. 然后根据最小斯坦纳树的定义, (x,fax ) 这条边的贡献是 x 子树内不同标号的个数目dif_. 那么显然有difi≤min(k,szi ), sz_i 表示子树大小. 可以通过构造让所有dif_i都取到最大值. 所以答案就是∑x=2n w[x][fax]∗min(szx,k) 时间复杂度O(n)
其实就是每条边尽多的利用~
而当前边可以用的最多次数 = min(子节点个数,可以分的最多的集合个数)
AC代码:
#include<cstdio>#include<vector>using namespace std;typedef long long LL;const int MAX = 1e6 + 10;int n,k,s[MAX],d[MAX];struct node{ int x,vl;};vector <node> v[MAX];void dfs(int x,int f){ s[x] = 1; for(int i = 0; i < v[x].size(); i++){ node w = v[x][i]; if(w.x == f) continue; d[w.x] = w.vl; dfs(w.x,x); s[x] += s[w.x]; }}int main(){ int a,b,c; while(~scanf("%d %d",&n,&k)){ for(int i = 1; i < n; i++){ scanf("%d %d %d",&a,&b,&c); v[a].push_back(node{b,c}); v[b].push_back(node{a,c}); } dfs(1,-1); LL ans = 0; for(int i = 2; i <= n; i++) ans += (LL) d[i] * min(s[i],k); printf("%lld\n",ans); for(int i = 1; i <= n; i++) v[i].clear(); } return 0;}
- HDU 6060 RXD and dividing 思维 + dfs
- 【HDU 6060 RXD and dividing】+ DFS
- HDU 6060 RXD and dividing【DFS】
- hdu 6060 RXD and dividing(dfs)
- -----dfs+思维 hdu 6060-RXD and dividing
- hdu 6060 RXD and dividing (dfs)
- hdu RXD and dividing(dfs)
- 【多校训练】hdu 6060 RXD and dividing dfs
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing
- [HDU]-6060 RXD and dividing
- HDU 6060 RXD and dividing
- HDU-6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu 6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu--6060-RXD and dividing
- HDU 6060 RXD and dividing
- 介绍 redis-Bit-Map 的相关命令和常用场景
- Codeforces835D-Palindromic characteristics(DP)
- MongoDB 聚合操作
- imx6 Linux Kernl添加spidev的设备节点
- maven 配置windows环境变量
- 【HDU 6060 RXD and dividing】+ DFS
- 分清数学库与浮点运算
- Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$Ac
- 【51Nod】1085
- 奥威数据可视化平台Power-BI -RFM分析找出活跃客户
- 事务策略
- ansible 管理索引
- java小试牛刀(一):碰撞吧,小球
- leveldb(十一):SSTable之2写sstable文件