RXD and dividing

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

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
【题意】给你一棵树,将节点[2,n]最多分为k份,再将1节点加入到每一份,将每一份的节点连接起来,权值之和加入ans,求最大化ans。
【分析】咋一看,发现很难,不会...但看了题解后仔细一想,就是个傻逼题啊...每一个 节点与他父亲节点之间的权值的贡献就是他子树分成的份数,那我们就最大化这个份   数就是了...

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e6+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,cas;
ll ans;
int sz[N];
vector<pii>edg[N];
void dfs(int u,int fa){
sz[u]=;
for(auto e : edg[u]){
int v=e.first;
int w=e.second;
if(v==fa)continue;
dfs(v,u);
sz[u]+=sz[v];
ans+=1LL*w*min(sz[v],k);
}
}
int main(){
//int T;
//scanf("%d",&T);
while(~scanf("%d%d",&n,&k)){
for(int i=;i<=n;i++)edg[i].clear();
for(int i=,u,v,w;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
edg[u].pb(mp(v,w));edg[v].pb(mp(u,w));
}
ans=;
dfs(,);
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. WPF绑定到集合
  2. Hadoop学习笔记—6.Hadoop Eclipse插件的使用
  3. PS色调均化滤镜的快捷实现(C#源代码)。
  4. NOIP2010引水入城[BFS DFS 贪心]
  5. JSR 303标准
  6. OWASP top10
  7. enmo_day_05
  8. oracle11g 修改字符集
  9. Android_开发工具的下载和开发环境的搭建
  10. Js获取URL中的QueryStirng字符串
  11. WPF制作的小型笔记本
  12. Oracle实战笔记(第七天)之PL/SQL进阶
  13. 【Idea】Intellij Idea debug 模式如果发现异常,即添加异常断点在发生异常处
  14. Vue 学习(1)
  15. HTML5常用API
  16. PCA和SVD最佳理解
  17. pyqt5-对文本样式进行操作
  18. qemu-img.exe 工具 简介
  19. CallByValue和CallByName区别
  20. English trip V1 - 5.That&#39;s Amazing! 棒极了! Teacher:Patrick Key: can or can&#39;t

热门文章

  1. Eclipse 使用mybatis generator插件自动生成代码
  2. Flying to the Mars
  3. JAVA多线程提高十二:阻塞队列应用
  4. 【BZOJ】1023: [SHOI2008]cactus仙人掌图 静态仙人掌(DFS树)
  5. hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
  6. nginx之日志设置详解
  7. HOJ 1108
  8. python 异常知识点
  9. 关于ORA-04091异常的出现原因,以及解决方案
  10. linux下使用privoxy将socks转为http代理