hdu6060 RXD and dividing 贪心
/**
题目:hdu6060 RXD and dividing
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060
题意:贪心
给定一颗树,n个节点,编号为1~n。将2~n编号的节点分成k份。
每一份分别和编号1的节点取并集。然后求每一份的节点连通的最小边权和;
然后k份获得的边权和加起来;问:求可以获得的k份边权和的总和的最大值。 思路:通过画树容易发现,假如k无穷大,如果节点x为根的子树有num个节点,那么x与x的父节点相连的那条边权最多加num次。 所以每个节点x与父节点相连的边的权值w的贡献为min(num,k)*w; num为以x为根的子树的总结点数。 */
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<time.h>
#include<random>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 1e9+;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+;
int n, k;
vector<P> G[maxn];
struct node
{
int sum;
int w;
}t[maxn];
void dfs(int r,int f)
{
int len = G[r].size();
t[r].sum = ;
for(int i = ; i< len; i++){
if(G[r][i].first==f) continue;
dfs(G[r][i].first,r);
t[G[r][i].first].w = G[r][i].second;
t[r].sum += t[G[r][i].first].sum;
}
}
int main()
{
while(scanf("%d%d",&n,&k)==)
{
for(int i = ; i <= n; i++) G[i].clear();
int u, v, w;
for(int i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(P(v,w));
G[v].push_back(P(u,w));
}
dfs(,-);
LL ans = ;
for(int i = ; i <= n; i++){
ans = (ans+(LL)min(t[i].sum,k)*t[i].w);
}
printf("%lld\n",ans);
}
return ;
}
hdu6060 RXD and dividing 贪心的更多相关文章
- 2017 Multi-University Training Contest - Team 3 hdu6060 RXD and dividing
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6060 题目: RXD and dividing Time Limit: 6000/3000 M ...
- HDU-6060 RXD and dividing
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060 多校的题目,每次只能写两道SB题,剩下的要么想不到,要么想到了,代码不知道怎么实现,还是写的 ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3
/* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...
- HDU 6060 17多校3 RXD and dividing(树+dfs)
Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...
- 2017ACM暑期多校联合训练 - Team 3 1005 RXD and dividing
题目链接 Problem Description RXD has a tree T, with the size of n. Each edge has a cost. Define f(S) as ...
- 2017 Multi-University Training Contest - Team 3 RXD and dividing(树)
题解: 其实贪心地算就可以了 一个最优的分配就是每条边权贡献的值为min(k, sz[x]),sz[x]是指子树的大小 然后最后加起来就是答案. #include <iostream> # ...
- HDU 6060 RXD and dividing(思维+计算贡献值)
http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意: 给定一棵 n 个节点的树,1 为根.现要将节点 2 ~ n 划分为 k 块,使得每一块与根节点形成 ...
- HDU 6060 RXD and dividing(LCA)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...
随机推荐
- 编译安装mysql-5.6.40
编译安装mysql-5.6.40 环境说明 系统版本 CentOS 7.2 x86_64 软件版本 mysql-5.6.40 [root@db01 ~]# mkdir -p /serv ...
- ESLint:can not ESLint annotation...
刚开始用webstorm开发VUE,提示这个东西: 安装一个npm库就可以了 命令行执行:npm install eslint -g
- TestNG 六 测试结果
一.成功.失败和断言 如果一个测试没有抛出任何异常就完成运行或者说抛出了期望的异常(参见@Test注解的expectedExceptions属性文档),就说,这个测试时成功的. 测试方法的组成常常包括 ...
- BEA公司的weblogic是什么?有什么特点?
转自:http://zhidao.baidu.com/link?url=J9obKwHhuh1sdLoBC3pILeaq1nz_tcpScggBNeS3D0GzAz9FI002vlS2xxJD4_z6 ...
- Kolla 4.0.0环境下VIP无法迁移问题排查
VRRP无法切换VIP的问题分析: Keepalived的配置文件:/etc/kolla/keepalived/keepalived.conf当中,nopreempt选项是影响切换的因素之一,另一个因 ...
- C#注释——爱你不是两三天
说到注释这个东东,我不得不说:爱你不是两三天,每天却想你很多遍...原来梁静茹同学这首歌不全然是情歌啊~ 一句注释也没有的一大片的代码有木有 看着那些无名者写的神秘代码,有没有骂一句,你妹的... ...
- PKCS7 的 attached 和 detached 方式的数字签名
搜遍了整个网络,都没有详细的说明.只在一个页面上有介绍,还模棱两可的,地址是:http://docs.oracle.com/cd/E19398-01/820-1228/gfnmj/index.html ...
- iOS UITableView获取特定位置的cell
代码地址如下:http://www.demodashi.com/demo/13307.html 一.tableView双级联动 以上两种效果比较类似,实现的关键在于都是需要获得在滑动过程中滑动到tab ...
- mongodb的基本语法(二)
一.聚集集合查询 1.查询所有记录 db.userInfo.find(); 相当于:select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一 ...
- Aptana插件在eclipse中安装