/**
题目: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. [置顶] (奇迹冬瓜)坦克大战[MFC框架]

    经过二次整合 重新放出MFC框架下的坦克大战 采用小窗口 多线程 双缓冲 动画帧化 碰撞检测 接口封装 混音 事件延迟等 力求做到代码与美工的双向化 开场动画 界面一 界面二 游戏界面 结束动画 零积 ...

  2. Centos&RHEL 6安装图形化

    Linux是一个多任务的多用户的操作系统,而在安装linux的时候经常遇到的问题-没有图形化桌面.在上节中我们演示了RHEL7安装图形化的过程,下面我们演示Centos6的图形化安装. 一.Cento ...

  3. 微博轻量级RPC框架Motan正式开源:支撑千亿调用

    支撑微博千亿调用的轻量级 RPC 框架 Motan 正式开源了,项目地址为https://github.com/weibocom/motan. 微博轻量级RPC框架Motan正式开源 Motan 是微 ...

  4. 关于web项目中的图片上传、并在前端显示问题(tomcat中配置文件上传虚拟路径)

    一.数据库存储 直接把图片的二进制码存到数据库,可参考blog:http://blog.csdn.net/hope2jiang/article/details/590733 直接存图片在mysql上面 ...

  5. vue - config(dev.env.js和prov.env.js)

    描述:配置产品模式.打包模式:开发还是打包,以最佳运行(不配置则有一个大大的Warning!!!) 官网:https://www.webpackjs.com/concepts/mode/

  6. poj1178 floyd+枚举

    http://poj.org/problem?id=1178 Description Centuries ago, King Arthur and the Knights of the Round T ...

  7. linux中的两个很重要的信号:SIGALRM信号和SIGCHID信号

    在进行堵塞式系统调用时.为避免进程陷入无限期的等待,能够为这些堵塞式系统调用设置定时器.Linux提供了alarm系统调用和SIGALRM信号实现这个功能.         要使用定时器.首先要安装S ...

  8. android项目解刨之时间轴

    近期开发的app中要用到时间轴这东西.须要实现的效果例如以下: 想想这个东西应该能够用listview实现吧. 然后近期就模拟着去写了: 首先写  listview的item的布局: listview ...

  9. 12-spring学习-基本表达式

    基本表达式 一,字面表达式 二,数学表达式 三,关系表达式 四,字符串表达式 String类中所有操作方法都是开发过程中最常用的. 五,正则表达式

  10. 编译ORBSLAM2 build_ros.sh,实现kinect2在ROS环境下运行ORBSLAM2

    //編譯ORBSLAM2 build_ros.sh參考:“http://www.cnblogs.com/bigzhao/p/6635770.html”1.source ~/.bashrc出現問題:ct ...