cf1042F. Leaf Sets(贪心)
题意
给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$
sol
考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上update的时候只返回最深的点的深度
然而却苦于写不出代码。。。
/* */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, K, ans = ;
vector<int> v[MAXN];
int dfs(int x, int fa) {
if(v[x].size() == ) return ;
vector<int> dis;
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
dis.push_back(dfs(to, x) + );
}
sort(dis.begin(), dis.end());
while(dis.size() >= ) {
int x = dis[dis.size() - ], y = dis[dis.size() - ];
if(x + y > K)
ans++, dis.pop_back();
else break;
}
return dis.back();
}
main() {
N = read(); K = read();
for(int i = ; i <= N - ; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
for(int i = ; i <= N; i++)
if(v[i].size() > ) {dfs(i, ); break;}
printf("%d", ans + );
return ;
}
/*
2 2 1
1 1
2 1 1
*/
cf1042F. Leaf Sets(贪心)的更多相关文章
- CF1042F Leaf Sets (贪心+树上构造)
题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数 手动yy的贪心竟然对的 对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶 ...
- CodeForces 1042 F Leaf Sets 贪心
Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...
- [CF1042F]Leaf Sets
题意:给定一棵$n$个点的树,将叶子节点分为数个集合使集合里点对最长距离不超过$k$,求最少集合数.($n\le1000000$) 首先我们可以想到,这道题并不是让你构造最优方案,因为只要把所有叶子节 ...
- 【CF1042F】Leaf Sets
[CF1042F]Leaf Sets 题面 洛谷 题解 对于一个根节点\(x\),考虑其子树内的所有\(lca\)为它的叶子节点到它的距离\(d_1<d2<...<d_m\). 那么 ...
- CF 1042 F. Leaf Sets
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
- 「CF1042F」Leaf Sets
传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...
- Generating Sets 贪心
H - Generating Sets Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- PullToRefresh------ListView的使用
第一步 :写出布局文件的设置 <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pu ...
- 牛叉之nc命令
nc是一款很不错的网络检测工具,以下是详细使用. 'nc.exe -h'即可看到各参数的使用方法. 基本格式:nc [-options] hostname port [ports] - nc -l - ...
- Vmware克隆Centos 不能上网的解决方案
问题:用Vmware克隆Centos 6.4后,发现系统内只有eth1,而且/etc/sysconfig/network-scripts/下只有,ifcfg-eth0文件,虽然可以上网,但无法设置静态 ...
- Jenkins配置有用摘抄笔记
使用jenkins配置.net mvc5网站自动构建全过程记录 转自:http://www.cnblogs.com/baiyunchen/p/4724350.html 持续集成是个简单重复劳动,人来 ...
- Spring入门第三十课
基于XML的方式配置事务 直接看代码: package logan.study.spring.tx.xml; public interface BookShopDao { //根据书号获取书的单价 p ...
- linux c段错误分析方法
from:http://blog.csdn.net/adaptiver/article/details/37656507 一. 段错误原因分析 1 使用非法的指针,包括使用未经初始化及已经释放的指针( ...
- Python中的变量,数据类型
Python中变量的命名规则:以字母和下划线开头,由字母,数字和下划线组成,区分大小写 Python中同样有加减乘除取余运算,还有一个运算符**,相当与幂运算,当然,幂运算的优先级要高于加减乘除 最后 ...
- 洛谷P3803 【模板】多项式乘法(FFT)
P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...
- 谢特——后缀数组+tire 树
题目 [题目描述] 由于你成功地在 $ \text{1 s} $ 内算出了上一题的答案,英雄们很高兴并邀请你加入了他们的游戏.然而进入游戏之后你才发现,英雄们打的游戏和你想象的并不一样…… 英雄们打的 ...
- ajax跨域问题解决方案(jsonp,cors)
跨域 跨域有三个条件,满足任何一个条件就是跨域 1:服务器端口不一致 2:协议不一致 3:域名不一致 解决方案: 1.jsonp 在远程服务器上设法动态的把数据装进js格式的文本代码段中,供客户端调用 ...