HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1893 Accepted Submission(s): 809
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.
1 2 3
2 3 4
2 4 5
2 5 6
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e6+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,cas;
ll ans;
int sz[N];
vector<pii>edg[N];
void dfs(int u,int fa){
sz[u]=;
for(auto e : edg[u]){
int v=e.first;
int w=e.second;
if(v==fa)continue;
dfs(v,u);
sz[u]+=sz[v];
ans+=1LL*w*min(sz[v],k);
}
}
int main(){
//int T;
//scanf("%d",&T);
while(~scanf("%d%d",&n,&k)){
for(int i=;i<=n;i++)edg[i].clear();
for(int i=,u,v,w;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
edg[u].pb(mp(v,w));edg[v].pb(mp(u,w));
}
ans=;
dfs(,);
printf("%lld\n",ans);
}
return ;
}
HDU 6060 RXD and dividing(dfs 思维)的更多相关文章
- HDU 6060 RXD and dividing(思维+计算贡献值)
http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意: 给定一棵 n 个节点的树,1 为根.现要将节点 2 ~ n 划分为 k 块,使得每一块与根节点形成 ...
- HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3
/* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...
- 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing
acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...
- HDU 6060 RXD and dividing(LCA)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点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 ...
- hdu6060 RXD and dividing 贪心
/** 题目:hdu6060 RXD and dividing 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意:贪心 给定一颗树,n个节点,编 ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
随机推荐
- 其他:strtok和sscanf结合输入读取一行整数
gets(buf); int v; char *p = strtok(buf," "); while(p) { sscanf(p,"%d",&v); p ...
- ZOJ 3964 NIM变形
LINK 题意:n堆石子,Alice 和 Bob 轮流取石子,谁不能再取或被对方取完为败.但是对于alice拥有限制:b=0此堆正常无限制:b=1此堆Alice只能取奇数个石子:b=2只能取偶数个石子 ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
- 组合数+逆元 A - Chat Group Gym - 101775A
题目链接:https://cn.vjudge.net/contest/274151#problem/A 具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要 ...
- 不相交集ADT--数组实现
不相交集是解决等价问题的一种有效的数据结构,之所以称之为有效是因为,这个数据结构简单(几行代码,一个简单数组就可以搞定),快速(每个操作基本上可以在常数平均时间内搞定). 首先我们要明白什么叫做等价关 ...
- C++ 模板特化以及Typelist的相关理解
近日,在学习的过程中第一次接触到了Typelist的相关内容,比如Loki库有一本Modern C++ design的一本书,大概JD搜了一波没有译本,英文版600多R,瞬间从价值上看到了这本书的价值 ...
- oracle命令生成AWR报告
--命令生成AWR报告oracle@linux:~> sqlplus / as sysdba SQL*Plus: Release 11.1.0.7.0 - Production on Fri A ...
- idea关于断点的补充
黑背景版: 先编译好要调试的程序.1.设置断点
- Nginx - 限制并发、限制访问速率、限制流量
1. 前言 本文针对 Nginx 的三个模块进行配置,并证实各自的功能特点: (1)limit_conn_zone 模块 - 限制同一 IP 地址并发连接数: (2)limit_request 模块 ...
- CRM 业务
1. 创建CRM项目 引入插件 创建数据库 from django.db import models from django.db import models class Department(mod ...