http://acm.hdu.edu.cn/showproblem.php?pid=6060

题意:

给定一棵 n 个节点的树,1 为根。现要将节点 2 ~ n 划分为 k 块,使得每一块与根节点形成的最小斯坦纳树的边权值总和最大。

思路:

这道题目应该往边的贡献值方向去思考,对于u(非1)结点,设它与它父亲结点的边为e,它的子节点分的块越多,那么e这条边的贡献值也就越大,因为每一分块都需要e这条边来连通。所以我们就要尽量让每条边的贡献值都最大。

下面图解一下:(分成 3 part的情况)

4、5、6、7由于超过了3,所以只能分成3部分,我们可以假设性的分为{4,5},{6},{7}这样3组,1和3是没有子树关系的,所以可以把它们合并进去,不会影响3结点边的贡献值,

现在又可以假设性的分为{4,5,8},{6,9},{7,10}这样三组,将2结点合并进去后可以变成{4,5,8},{2,6,9},{7,10}这样三组,此时1、2、3、4、5、6、7、8、9这几个结点的边都已经达到了它们所能达到的最大贡献值。

依次向上分析即可。

所以,我们可以得出结论,每条边的最大贡献值就是min(sz[u],k),最后将所有边的贡献值加起来即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, k; int sz[maxn];
int val[maxn];
vector<pll> G[maxn]; void dfs(int u, int fa)
{
sz[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
if(v==fa) continue;
val[v]=G[u][i].second;
dfs(v,u);
sz[u]+=sz[v];
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
for(int i=;i<=n;i++) G[i].clear(); for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
} dfs(,-);
ll ans=;
for(int i=;i<=n;i++)
ans+=(ll)val[i]*min(k,sz[i]);
printf("%lld\n",ans);
}
return ;
}

HDU 6060 RXD and dividing(思维+计算贡献值)的更多相关文章

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

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

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

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

  3. HDU 6060 RXD and dividing(LCA)

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

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

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

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

  6. hdu6060 RXD and dividing 贪心

    /** 题目:hdu6060 RXD and dividing 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意:贪心 给定一颗树,n个节点,编 ...

  7. 2017 多校3 hdu 6061 RXD and functions

    2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...

  8. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...

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

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

随机推荐

  1. Spark Storage(一) 集群下的区块管理

    Storage模块 在Spark中提及最多的是RDD,而RDD所交互的数据是通过Storage来实现和管理 Storage模块整体架构 1. 存储层 在Spark里,单节点的Storage的管理是通过 ...

  2. maven工程插件配置

    <build> <!-- 该级工程会加载插件,放在父工程里 --> <plugins> <!-- 资源文件拷贝插件 --> <plugin> ...

  3. MVC html.beginform & ajax.beginform

    1.指定表单提交方式和路径等 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { nam ...

  4. <span> 标签

    <span> 标签被用来组合文档中的行内元素. 如果不对 span 应用样式,那么 span 元素中的文本 与 其他文本不会有任何视觉上的差异.尽管如此,上例中的 span 元素仍然为 p ...

  5. html03

    快速开发指南:1.新建页面之后,编写外部样式表文件,引入到HTML页面中2.用浏览器打开页面->F12->sources->打开css文件 右边编写样式,左边查看效果3.样式编写完成 ...

  6. 转载的 Linux下chkconfig命令详解

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  7. tensorflow训练自己的数据集实现CNN图像分类1

    利用卷积神经网络训练图像数据分为以下几个步骤 读取图片文件 产生用于训练的批次 定义训练的模型(包括初始化参数,卷积.池化层等参数.网络) 训练 1 读取图片文件 def get_files(file ...

  8. LoadRunner11支持的浏览器小结-Loadrunner11打不开IE浏览器的问题

    http://www.cnblogs.com/qmfsun/p/4807237.html

  9. firefox历史版本下载链接

    http://ftp.mozilla.org/pub/firefox/releases firefox版本42以上的用不了firebug,需要装版本42以下的,否则用不了

  10. ubuntu14.04无法安装Curl,需要先升级sudo apt-get update

    ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...