原题链接:http://codeforces.com/contest/1042/problem/F

Leaf Sets

You are given an undirected tree, consisting of nnn vertices.

The vertex is called a leaf if it has exactly one vertex adjacent to it.

The distance between some pair of vertices is the number of edges in the shortest path between them.

Let’s call some set of leaves beautiful if the maximum distance between any pair of leaves in it is less or equal to kkk.

You want to split all leaves into non-intersecting beautiful sets. What is the minimal number of sets in such a split?

Input

The first line contains two integers nnn and k(3≤n≤106,1≤k≤106)k (3≤n≤10^6, 1≤k≤10^6)k(3≤n≤106,1≤k≤106) — the number of vertices in the tree and the maximum distance between any pair of leaves in each beautiful set.

Each of the next n−1n−1n−1 lines contains two integers viv_ivi​ and ui(1≤vi,ui≤n)u_i (1≤v_i,u_i≤n)ui​(1≤vi​,ui​≤n) — the description of the iii-th edge.

It is guaranteed that the given edges form a tree.

Output

Print a single integer — the minimal number of beautiful sets the split can have.

Examples
input

9 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
3 9

output

2

input

5 3
1 2
2 3
3 4
4 5

output

2

input

6 1
1 2
1 3
1 4
1 5
1 6

output

5

Note

Here is the graph for the first example:

题解

对于每个点维护最深的叶子离自己的距离,向上合并时,将所有儿子按最深的叶子离自己的距离排序,从深到浅遍历尝试合并,如果相邻的两个距离加起来大于kkk,便无法合并,向上传也没有意义,于是就将该儿子及其子树单独分成一个集合,答案+1,最后返回在删去所有分离的儿子后最深的叶节点离自己的距离。

代码
#include<bits/stdc++.h>
#define add(a,b) mmp[a].push_back(b)
using namespace std;
const int M=1e6+5;
int ans,n,k;
vector<int>mmp[M];
int dfs(int f,int v)
{if(mmp[v].size()==1)return 0;vector<int>tmp;for(int i=mmp[v].size()-1;i>=0;--i)if(mmp[v][i]!=f)tmp.push_back(dfs(v,mmp[v][i])+1);sort(tmp.begin(),tmp.end());for(int siz;(siz=tmp.size())>1&&tmp[siz-1]+tmp[siz-2]>k;++ans,tmp.pop_back());return tmp.back();
}
void in(){scanf("%d%d",&n,&k);for(int i=1,a,b;i<n;++i)scanf("%d%d",&a,&b),add(a,b),add(b,a);}
void ac(){for(int i=1;i<=n;++i)if(mmp[i].size()>1)dfs(0,i),printf("%d",ans+1),exit(0);}
int main(){in(),ac();}

CF1042F Leaf Sets相关推荐

  1. [CF1042F]Leaf Sets

    题目大意:给定一棵$n$个点的树,将叶子节点分为数个集合,使集合里点对最长距离不超过$k$,求最少集合数. 题解:贪心,发现将叶子节点分成集合等于把节点划分集合,答案是一样的.因为一定有一个点,到非叶 ...

  2. CF1042F Leaf Sets (贪心+树上构造)

    题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数 手动yy的贪心竟然对的 对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶 ...

  3. TOJ 4008 The Leaf Eaters

    |A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C| 这个是集合的容斥,交集差集什么的,这个在概率论经常用到吧 4008: The Leaf Eaters   T ...

  4. lists,tuples and sets of Python

    (python2.7.x) Lists 列表 列表是一个有序序列(集合),可变的(可以修改),可以理解为其他语言中的数组类型,但是列表更灵活更强大. 列表由方括号[]来定义的,它的元素可以是任意类型或 ...

  5. [pcl::VoxelGrid::applyFilter] Leaf size is too small for the input dataset. Integer indices would ov

    1. 报错日志: Python-pcl 点云下采样时报错如下: [pcl::VoxelGrid::applyFilter] Leaf size is too small for the input d ...

  6. H.264 picture parameter sets成员值含义学习笔记

    -picture parameter sets 1. pic_parameter_set_id pic_parameter_set_id指明了在切片头中对应的某个psp.pic_parameter_s ...

  7. 美团(Leaf)分布式ID生成器,好用的一批!

    不了解分布式ID的同学,先行去看<一口气说出 9种 分布式ID生成方式,面试官有点懵了>温习一下基础知识,这里就不再赘述了 美团(Leaf) Leaf是美团推出的一个分布式ID生成服务,名 ...

  8. 粗糙集(Rough sets)、模糊逻辑(Fuzzy Logic)

    第一章  粗糙集理论简介 第一节  粗糙集理论的产生与应用背景 在20世纪70年代,波兰学者Z.Pawlak和一些波兰科学院.波兰华沙大学的逻辑学家们,一起从事关于信息系统逻辑特性的研究,粗糙集理论就 ...

  9. zoj——3556 How Many Sets I

    How Many Sets I Time Limit: 2 Seconds      Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...

  10. Leaf:GO语言游戏框架介绍及入门

    Leaf 是一个由 Go 语言(golang)编写的开发效率和执行效率并重的开源游戏服务器框架.Leaf 适用于各类游戏服务器的开发,包括 H5(HTML5)游戏服务器. Leaf 的关注点: 良好的 ...

最新文章

  1. 计算机应用基础知识点.pdf,《计算机应用基础》复习知识点.pdf
  2. JS动态获取当前时间
  3. 【转】深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing) 第四节 参数传递对堆栈的影响 2
  4. angular 字符串转换成数字_Angular日期在TypeScript中格式化转换应用
  5. 机器学习笔记(十三)——隐马尔科夫模型
  6. 可用于企业的 7 个最佳开源 Web 服务器
  7. 第三:项目框架设计介绍
  8. 如何设置qq支持临时会话聊天
  9. springboot基于协同过滤算法的书籍推荐毕业设计源码101555
  10. 安装远程服务器管理工具(RSAT-Remote Server Administration Tools)
  11. 云可信 数未来| 中国电子云总部揭牌
  12. rknn3399pro 2小时入门指南(八)详细教程指导你一步到位训练得到yolov3 rknn模型
  13. banq修复_SM3271AB_慧荣SM3271AB U盘量产加密及修复教程 - 系统部
  14. php redis访问量,redis实现点击量浏览量
  15. 强大且免费的文本转换工具,word转其他格式,epub转换
  16. Vue3的filter过滤器代替方法
  17. 如果你身边有程序员 今天对他好一点
  18. Hacker_kid靶机打靶过程及思路
  19. (简单)华为畅玩5C NEM-AL00的usb调试模式在哪里打开的教程
  20. 各项的语言资源,从入门到精通

热门文章

  1. Idea和Eclipse快捷键对比
  2. 刷题记录 CF每日一题打卡 2020.6月7-6月13
  3. 462.最少移动次数使数组元素相等II  (力扣leetcode) 博主可答疑该问题
  4. typecast java_Delphi设置无效的Typecast
  5. js刷新当前页面的几种方式
  6. read from and write to file
  7. HDU 1325 Is It A Tree?(并查集)
  8. 动态创建和移除HTML标签
  9. objective-c 编程总结(第一篇)字符串操作
  10. HTML5 - 搭建移动Web应用