HDU 6060 17多校3 RXD and dividing(树+dfs)
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T.
he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,
where ⋃Si={2,3,…,n} and for all different i,j , we can conclude that Si⋂Sj=∅.
Then he calulates res=∑ki=1f({1}⋃Si).
He wants to maximize the res.
1≤k≤n≤106
the cost of each edge∈[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.
The next n−1 lines consists of 2 integers, a,b,c, means a tree edge (a,b) with cost c.
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n≤100.
给一个k,表示有k个集合,我们需要把2,3,4,…n号节点放入集合,要保证k个集合的并集等于{2,3,4,5…n},
并且集合互不相交。(集合可以为空)
然后每次取一个集合Si与{1}求并,得到比如{1,2,3},那么tempi = f({1,2,3});f({1}并Si)的意思是把集合内的
所有点连接起来的边的权值和。最后把所有权值和相加的到答案。
最后问你能够得到最大的答案。
我们要想得到最大的答案,那么就要尽可能的去利用这些边,也就是尽可能重复计算这些边。
那么我们想,假设先从叶子节点开始,把这些叶子节点放入一个集合,那么这个集合的temp值就会把所有的边都算一遍。那么下次我们取所有叶子节点的父亲,放入一个集合,那么这个集合的temp值会把除了叶子节点到父亲的那条那边的其他所有边都算一遍。因为集合可以为空,以此类推,我们就可以得到最大的答案。但是如果遇到集合不够的情况,就把剩下的所有点加入最后一个集合。
那么有以上分析,其实就是算每条边会算多少次,比如叶子节点到父亲的那条边会算一次。其实一条边会算多少次跟某个点的所有子孙节点个数有关,就比如样例中,2号点有3个子孙节点, 那么2号点连接父节点的那条边会算3+1次。3号点有0个子孙节点,那么3号点连接父节点的那条边会算0+1次。
那么其实问题就是转化为求每个点的子孙节点个数,然后算出每条边要重复计算的次数即可。
#include<cstdio>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define MAXN 1000005 struct Edge
{
int v,w;
};//Edge[u]里面存着u连接的v和这条边的权值
vector<Edge>vec[MAXN];//容器存每个u对应的v和w
int size[MAXN];//每个节点的子节点数
int weight[MAXN];//weight[v]记录v点和它父节点的边值 void dfs(int u,int pre)//u是当前点,pre是父节点
{
size[u]=;//它本身
int len=vec[u].size();
int vv;//u的子节点
for(int i=;i<len;i++)
{
vv=vec[u][i].v;
weight[vv]=vec[u][i].w;//记录v点和它父节点的边值
dfs(vv,u);
size[u]+=size[vv];
}
} int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=;i<=n;i++)
{
vec[i].clear();
size[i]=;
weight[i]=;
}
Edge tmp;
for(int i=;i<=n-;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
tmp.w=w;
//tmp.v=u;
//vec[v].push_back(tmp);
tmp.v=v;
vec[u].push_back(tmp);
}
dfs(,);//深搜找出一个子树节点的个数
long long sum=;
for(int i=;i<=n;i++)//只分2-n
{
sum+=(long long)weight[i]*min(size[i],k);
}
printf("%lld\n",sum);
}
return ;
}
这种写法比较好理解,但我感觉如果遇到不是按照父子顺序给出的权值的话,这种写法就不太适合,邻接表会好一点
HDU 6060 17多校3 RXD and dividing(树+dfs)的更多相关文章
- HDU 6063 17多校3 RXD and math(暴力打表题)
Problem Description RXD is a good mathematician.One day he wants to calculate: ∑i=1nkμ2(i)×⌊nki−−−√⌋ ...
- HDU 6066 17多校3 RXD's date(超水题)
Problem Description As we all know that RXD is a life winner, therefore he always goes out, dating w ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
- HDU 6143 17多校8 Killer Names(组合数学)
题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...
- HDU 6045 17多校2 Is Derek lying?
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- HDU 6124 17多校7 Euler theorem(简单思维题)
Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...
- HDU 3130 17多校7 Kolakoski(思维简单)
Problem Description This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This seq ...
- HDU 6038 17多校1 Function(找循环节/环)
Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...
随机推荐
- Oracle中的instr()函数 详解及应用
1)instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( strin ...
- myeclipse debug模式 报错source not found
myeclipse debug模式下,启动报错 source not found:SignatureParser.current() line: 解决方法:将debug视图下的右上方的jar有断点的地 ...
- 【LeetCode】大数相乘
1. 模拟手工计算 原理: 将 string 反转存储在 int 数组中,如 A = 17 = (7, 1),B = 25 = (5, 2),亦即幂表示法,幂次是从低位到高位. 作逐位相乘,即 ai ...
- 解决iOS第三方SDK之间重复的symbols问题
前言:今天公司项目准备使用高德导航,其中用到了高德3D地图SDK,然后就出现bug了.在真机上可以完美运行,但是在模拟器上,就出现了一大片的bug:提示有82个Duplicate symbols,仔细 ...
- Linux查看某个命令属于哪个包
有时修我们需要某个命令但其没有安装,提供该命令的包名也与命令名相差很大直接查找命令名找不到包,如rexec. 此时我们就非常需要这样一个工具:可以根据最终的命令查找提供该命令的软件包. 类型 命令 说 ...
- summary_22rd Nov 2018
一. 列表:记录同种属性的多个值 定义:在[]中用逗号分隔开多个任意的值 类型转换:L=list( ) 括号中的内容必须是可迭代类型,包括字符串,列表,字典等 常用操作和内置方法: 1.按照索引位置 ...
- Struts 2 初步入门(二)
Struts 2 动态方法调用 1.在HelloWorldAction中添加两个新的方法如下: import com.opensymphony.xwork2.ActionSupport; public ...
- LY.JAVA面向对象编程.包的概述、导包
2018-07-18 08:46:57 导包:
- 逆袭之旅DAY17.东软实训.Oracle.PLSQL.过程,函数,包,练习
2018-07-13 14:54:46 --1.创建一个包,包含一个为雇员加薪的过程,一个为雇员减薪的过程 CREATE OR REPLACE PACKAGE pac_test1 IS PROCEDU ...
- re随机模块应用-生成验证码(无图片)
方法一,通过choice方式生成验证码 此方法生成每次调用crate_code()会生成三个随机数,然后再三个随机数中选择一个,资源调用相对多些 import random def v_code(co ...