2017多校3 1005 RXD and dividing
来源:互联网 发布:微信一键加人软件 编辑:程序博客网 时间:2024/06/16 14:03
http://acm.hdu.edu.cn/showproblem.php?pid=6060
求一棵树中从2-n的点分成k部分,求每一部分加上1这个根节点的最小生成树长度和的最大值。
可以理解成求每条边的贡献,最大边肯定是选择越多越好,那么我们就可以去寻找每一个根节点下的节点数与k的比较,如果k小,那该根节点的边就可以认为会贡献k次,如果是节点数小,那么就是过节点数次,dfs去寻找节点数就可以了。
#include<iostream>#include<cstring>#include<cstdio>using namespace std;const long long int N=10000007;struct node{ int en; int v; int next;}e[N];int n,k;int cnt;int _hash[N];long long int ans;void _push(int op,int ed,int cost){ e[++cnt].en=ed; e[cnt].v=cost; e[cnt].next=_hash[op]; _hash[op]=cnt; }int find(int op,int ed,int cost){ int i; int sum=1; for(i=_hash[ed];i;i=e[i].next) { int too=e[i].en; if(too!=op) sum+=find(ed,too,e[i].v); } if(sum>k)ans+=(long long int )cost*k; else ans+=(long long int)cost*sum; return sum;}int main(){ while(~scanf("%d%d",&n,&k)) { ans=0; cnt=0; int op,ed,cost; int i; memset(_hash,0,sizeof(_hash)); for(i=1;i<n;i++) { scanf("%d%d%d",&op,&ed,&cost); _push(op,ed,cost); _push(ed,op,cost); } find(0,1,0); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- 2017多校3 1005 RXD and dividing
- 多校3 hdu6060 RXD and dividing
- hdu 多校 RXD and dividing
- 2017年多校赛第三场 1005 RXD and dividing(DFS)
- 2017多校训练Contest3: 1005 RXD and dividing hdu6060
- Hdu6060 RXD and dividing(2017多校第3场)
- HDU-2017 多校训练赛3-1005-RXD and dividing
- HDU6060-RXD and dividing
- HDU6060 RXD and dividing
- RXD and dividing
- hdu6060 RXD and dividing
- hdu6060 RXD and dividing
- HDU6060-RXD and dividing
- RXD and dividing
- hdu6060 RXD and dividing
- RXD and dividing
- hdu6060 RXD and dividing
- hdu RXD and dividing(dfs)
- 杭电ACM 1465 不容易系列之一(错排)
- Color the ball (线段树)
- 文章标题
- local host is: "node5/172.16.100.115"; destination host is: "node5":9000;
- XMind之走进中国军队
- 2017多校3 1005 RXD and dividing
- 设计模式-工厂模式
- Android Volley完全解析(三),定制自己的Request
- Supermicro SYS-4028GR-TR +Nvidia tesla P4 +Ubuntu 16.04 +CUDA 8.0 深度学习环境搭建
- [codeforces] Gym
- Spring AOP 和 动态代理技术
- C++基础2:类与对象
- LIS&&LCS
- 算法学习—排序篇