/*
HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3
题意:
给一个 n 个节点的树,要求将 2-n 号节点分成 k 部分,然后将每一部分加上节点 1,
每一个子树的 val 为最小斯坦纳树,求总的最大 val
分析:
考虑每条边下面所在的子树,大小为num
由于该子树至多被分成 k 块,故该边最多贡献 k 次,贡献次数当然是越多越好
所以每条边的贡献为 w * min(k, num)
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
#define LL long long
struct Edge {
int to, w, next;
}edge[N<<1];
int head[N], tot;
void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
void addedge(int u, int v, int w)
{
edge[tot].to = v; edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
int sum[N], w[N];
int dfs(int u, int pre)
{
sum[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v == pre) continue;
sum[u] += dfs(v, u);
w[v] = edge[i].w;
}
return sum[u];
}
int k, n;
int main()
{
while (~scanf("%d%d", &n, &k))
{
init();
int a, b, c;
for (int i = 1; i < n; i++)
{
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c);
addedge(b, a, c);
}
dfs(1, 1);
LL ans = 0;
for (int i = 2; i <= n; i++)
{
ans += (LL)min(sum[i], k) * w[i];
}
printf("%lld\n", ans);
}
}

  

HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3的更多相关文章

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

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

  2. 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing

    acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...

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

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

  4. HDU 6060 RXD and dividing(LCA)

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

  5. HDU 6061 - RXD and functions | 2017 Multi-University Training Contest 3

    每次NTT都忘记初始化,真的是写一个小时,Debug两个小时- - /* HDU 6061 - RXD and functions [ NTT ] | 2017 Multi-University Tr ...

  6. HDU 6063 - RXD and math | 2017 Multi-University Training Contest 3

    比赛时候面向过题队伍数目 打表- - 看了题解发现确实是这么回事,分析能力太差.. /* HDU 6063 - RXD and math [ 数学,规律 ] | 2017 Multi-Universi ...

  7. HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9

    /* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...

  8. 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。

    /** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...

  9. 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟

    /** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...

随机推荐

  1. Design Excel Sum Formula

    Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...

  2. lua语法介绍(二)

    一.语法简要 在学习任何语法之前,我们都需要知道该门语言是怎样定义的,是怎样运行的,话说白了,就是到了人家的山头得唱人家山头的歌.下面介绍lua的语法 1.变量的定义 特点: 1.变量在使用前必须声明 ...

  3. Docker下mysql容器开启binlog日志(保留7天)

    现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...

  4. P1040 加分二叉树(区间DP)

    (点击此处查看原题) 解题思路 题目已经给出了树的中序遍历,因此我的想法是利用中序遍历的特点:若某子树的根结点为k,那么k之前的结点组成这一子树的左子树,k之后的结点组成这一子树的右子树,可以通过不断 ...

  5. DAG添边定理

    让DAG变成强连通就是把尾和头连起来,也就是入度和出度为0的点,添的边数:max(num_in==0,num_out==0)

  6. Python面向对象中的继承、多态和封装

    Python面向对象中的继承.多态和封装 一.面向对象的三大特性 封装:把很多数据封装到⼀个对象中,把固定功能的代码封装到⼀个代码块, 函数,对象, 打包成模块. 这都属于封装思想. 继承:⼦类可以⾃ ...

  7. JS执行顺序问题

    JavaScript执行引擎并非一行一行地分析和执行程序,而是一段一段地分析执行的.而且在分析执行同一段代码中,定义式的函数语句会被提取出来优先执行.函数定义执行完后,才会按顺序执行其他代码. 先看看 ...

  8. [转载]torch参数:torch.backends.cudnn.benchmark的意义

    [转载]torch参数:torch.backends.cudnn.benchmark的意义 来源:https://zhuanlan.zhihu.com/p/73711222 完整版请看原文,这里只截取 ...

  9. LeetCode:596.超过5名学生的课

    题目链接:https://leetcode-cn.com/problems/classes-more-than-5-students/ 题目 有一个 courses 表 ,有: student (学生 ...

  10. 0502 xss 实验

    0x01 dvwa xss(reflected) 1.1 Security Level: low use the typical <script>alert(1)</script&g ...