/**
题目: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. Spring框架学习(2)IOC学习

    内容源自:IOC理解   spring ioc注入的三种方式  ioc工厂bean深入理解 耦合性,在java中表现为类之间的关系,耦合性强说明类之间的依赖关系强: 侵入性:框架对代码的侵入: 在传统 ...

  2. MVVM和MVC的区别,以及MVVM的缺点

    MVVM和MVC的区别 MVC和MVVM的区别其实并不大.都是一种设计思想. 主要就是MVC中Controller演变成MVVM中的viewModel. MVVM主要解决了MVC中大量的DOM操作使页 ...

  3. Spring Boot环境下自定义shiro过滤器会过滤所有的url的问题

    在配置shiro过滤器时增加了自定义的过滤器,主要是用来处理未登录状态下返回一些信息 //自定义过滤器 Map<String, Filter> filtersMap = new Linke ...

  4. Windows开机出现提示“nwsvc.exe”错误怎么办

    开机时出现错误提示: Microsoft Visual C++ Runtime Library Runtime Error! Program: C:\Windows\system32\nwsvc.ex ...

  5. C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)

    <#@ template language="C#" #> <#@ output extension=".cs" #> <#@ a ...

  6. Wireshark Lua: 一个从RTP抓包里导出H.264 Payload,变成264裸码流文件(xxx.264)的Wireshark插件

    Wireshark Lua: 一个从RTP抓包里导出H.264 Payload,变成264裸码流文件(xxx.264)的Wireshark插件 在win7-64, wireshark Version ...

  7. vue - helloWorld

    1.cdn概念:cdn全称内容分发网络,也是加速服务之一. 2.数据绑定:{{data}} 3.el属性(挂载对象):el:标签任意(例如:#app,.app,app) 4.data:{} => ...

  8. angular开发中对请求数据层的封装

    代码地址如下:http://www.demodashi.com/demo/11481.html 一.本章节仅仅是对angular4项目开发中数据请求封装到model中 仅仅是在项目angular4项目 ...

  9. hibernate的配置, 增删改查

    路径:查找路径 实际上都是查找编译后的对应的路径,在bin文件夹中总 增删改必须开启事务才行 hibernate加载文件的两种方式 configure 1.引包 antlr-2.7.6.jar bac ...

  10. esriControlsMousePointer常量

    控制鼠标指针选项. 不变 值 描述 esriPointerParentWindow -1 指针指定的父窗口或窗体. esriPointerDefault 0 默认指针,箭头一样. esriPointe ...