CF1042F Leaf Sets (贪心+树上构造)
题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数
手动yy的贪心竟然对的
对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶节点到$i$节点的最长距离
那么,对于每个节点,把它的子节点按照$ma[i]$排序,那么如果这个点的子树不需要额外的分组,就要保证最大的$ma[v1]$和次大的$ma[v2]$之间的距离小于等于K
如果不满足,说明需要对子树内的节点进行额外分组
根据贪心的思想,选择ma最大的子节点$v1$,那么就从小往大一直找满足$ma[v1]+ma[vj]<=K$的点,当不满足条件时,说明刚才找过的小节点和那个较大的节点可以分成一组。接下来,要看次大$v2$的点能否满足更次大$v3$能否满足$ma[v2]+ma[v3]<=K$,找到说明可行,回溯。否则要继续刚才的过程,直到剩余子节点之间的最长距离<=K
因为每个节点只会以这种方式被遍历到一次,所以并不需要二分
1号节点可能是叶节点,所以不能直接把1当成根
另外,如果根节点的ma>1,说明根节点还剩下一些节点没被分组,把它们分到一组即可
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
#define N 1001000
#define uint unsigned int
#define inf 0x3f3f3f3f3f3f3fll
using namespace std;
//re
int gint()
{
int ret=,fh=;char c=getchar();
while(c<''||c>''){if(c=='-')fh=-;c=getchar();}
while(c>=''&&c<=''){ret=(ret<<)+(ret<<)+c-'';c=getchar();}
return ret*fh;
} int n,m,cte,num,S;
int head[N],fa[N],inc[N];
struct Edge{int to,nxt;}edge[N*]; void ae(int u,int v){
cte++;edge[cte].to=v,inc[v]++;
edge[cte].nxt=head[u],head[u]=cte;
} vector<int>to[N];
int ma[N];
int cmp(int x,int y){return ma[x]<ma[y];}
int solve(int u){
int ans=,l,r;
for(int j=head[u];j;j=edge[j].nxt)
{
int v=edge[j].to;
if(v==fa[u]) continue;
to[u].push_back(v);
ans+=solve(v);
}
int tot=to[u].size();
sort(to[u].begin(),to[u].end(),cmp);
if(!tot){ma[u]=;return ;}
if(tot==){ma[u]=ma[to[u][tot-]];}
else if(ma[to[u][tot-]]+ma[to[u][tot-]]<=m)
ma[u]=ma[to[u][tot-]];
else{
l=,r=tot-;
while(r>&&l<r&&ma[to[u][r]]+ma[to[u][r-]]>m){
for(;l<r&&ma[to[u][r]]+ma[to[u][l]]<=m;l++);
r--,ans++;
}ma[u]=ma[to[u][r]];
}ma[u]+=(ma[u]>?:);return ans;
} int main()
{
scanf("%d%d",&n,&m);
int x,y;
for(int i=;i<n;i++)
x=gint(),y=gint(),ae(x,y),ae(y,x);
for(int i=;i<=n;i++)
if(inc[i]!=) {S=i;break;}
dep[S]=,dfs1(S,-);
tp[S]=,dfs2(S);
int ans=solve(S);
if(ma[S]->) ans++;
printf("%d\n",ans);
return ;
}
CF1042F Leaf Sets (贪心+树上构造)的更多相关文章
- cf1042F. Leaf Sets(贪心)
题意 题目链接 给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$ sol 考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上 ...
- 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\). 于是我们把前 \( ...
- CF #296 (Div. 1) B. Clique Problem 贪心(构造)
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
随机推荐
- Git clone时出现Please make sure you have the correct access rights and the repository exists.问题已解决。
看了好多资料终于搞定了git 中clone命令报错这个问题,废话不多说直接上步骤希望对大家有帮助. 1 删除.ssh文件夹(直接搜索该文件夹)下的known_hosts(手动删除即可,不需要git ...
- snmp--CentOS安装Net-SNMP小计
http://blog.csdn.net/tmpbook/article/details/39620549
- [Design]Adobe CS6 2%错误问题
错误描述:FATAL: Payload '{3F023875-4A52-4605-9DB6-A88D4A813E8D} Camera Profiles Installer 6.0.98.0' info ...
- NHibernate之旅(18):初探代码生成工具使用
本节内容 引入 代码生成工具 结语 引入 我们花了大量的篇幅介绍了相关NHibernate的知识.一直都是带着大家手动编写代码,首先创建数据库架构.然后编写持久化类和映射文件,最后编写数据操作方法.測 ...
- HDU 2138
这题用MILLER测试应该是不可避免的. #include <iostream> #include <cstdio> #include <stdlib.h> #in ...
- [React] Reference a node using createRef() in React 16.3
In this lesson, we look at where we came from with refs in React. Starting with the deprecated strin ...
- Android适屏
总结一下自己的适屏经验,仅仅希望自己不断进步,不断完好,假设有热心肠的"前辈"指导一下,不胜感激! Android5.0已经出来了,说是这个版本号对Android屏幕适配做了非常多 ...
- (十进制高速幂+矩阵优化)BZOJ 3240 3240: [Noi2013]矩阵游戏
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3240 3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec M ...
- asp.net学习指南
个人总结了一些不错的基础视频教程 视频链接地址(猛戳这里)
- 粘性固定属性 -- position:sticky
概述 position: sticky,这是一个比较容易忽略的css3 position 新属性,它的作用即为实现粘性布局,它是 relative 与 fixed 的结合. 用法 默认情况下,其表现为 ...