/**
题目: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 贪心的更多相关文章

  1. 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 ...

  2. HDU-6060 RXD and dividing

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060   多校的题目,每次只能写两道SB题,剩下的要么想不到,要么想到了,代码不知道怎么实现,还是写的 ...

  3. HDU 6060 RXD and dividing(dfs 思维)

    RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  4. HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3

    /* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...

  5. 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 ...

  6. 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 ...

  7. 2017 Multi-University Training Contest - Team 3 RXD and dividing(树)

    题解: 其实贪心地算就可以了 一个最优的分配就是每条边权贡献的值为min(k, sz[x]),sz[x]是指子树的大小 然后最后加起来就是答案. #include <iostream> # ...

  8. HDU 6060 RXD and dividing(思维+计算贡献值)

    http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意: 给定一棵 n 个节点的树,1 为根.现要将节点 2 ~ n 划分为 k 块,使得每一块与根节点形成 ...

  9. HDU 6060 RXD and dividing(LCA)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...

随机推荐

  1. Java Web----EL(表达式语言)详解

     Java Web中的EL(表达式语言)详解 表达式语言(Expression Language)简称EL,它是JSP2.0中引入的一个新内容.通过EL可以简化在JSP开发中对对象的引用,从而规范页面 ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何修改某个轴的数值单位

    在某个轴上双击,切换到Settings,然后可以再Unit中修改为角度,弧度,mm     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/aceta ...

  3. Spring官方下载地址

    改版后的Spring官方网站下载地址找不到了,汗~~ 可以通过该链接下载对应的包:http://repo.spring.io/milestone/org/springframework/ Spring ...

  4. Apatche httpd + Django + Mysql web server 搭建

    Required: httpd: 2.4.18 django : 1.8.7 mysql: 5.7.10 MySQL-python: 1.2.3 mod_wsgi: 4.4.21 Offical Do ...

  5. AngularJS监听DOM加载完毕

    直接上代码: Module.directive('renderFinish', function ($timeout) { //renderFinish自定义指令 return { restrict: ...

  6. 《暗黑世界V1.3》数据库表说明文档

    <暗黑世界V1.3>数据库表说明文档 (下载地址:http://www.9miao.com/forum.php?mod=viewthread&tid=38821&highl ...

  7. git 关联远程分支

    问题解析: git本地新建一个分支后,必须要做远程分支关联.如果没有关联, git 会在下面的操作中提示你显示的添加关联.关联目的是如果在本地分支下操作: git pull, git push ,不需 ...

  8. 使用 dbdeploy.net 管理数据库变更

    使用 dbdeploy.net 管理数据库变更 没有包含数据库的持续集成都是假的.这可不是我说的.一直以来都没能找到一个理想的数据库变更管理工具.直到转了 java 再回来,才发现 dbdeploy ...

  9. Android--从零开始开发一款文章阅读APP

    代码地址如下:http://www.demodashi.com/demo/11212.html 前言 本案例已经开源!如果你想免费下载,可以访问我的Github,所有案例均在上面,只求给个star.当 ...

  10. 【微信小程序】转载:微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...