传送门
Solution
考虑将每一个子树分开处理,那么对于子树内距离(ge K)的就分一块,然后这样子贪心不断往上走就是对的我也不知道为什么
大致可以类比一下树分块的做法。
Code
/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=1000010;
int n,k,d[N],ans;
vector<int>G[N];
int dfs(int u,int ff){
if(d[u]==1)return 0;
int top=0;vector<int>sta;
for(int v:G[u]){if(v==ff)continue;sta.push_back(dfs(v,u)+1);}
sort(sta.begin(),sta.end());
int now=sta.size()-1;
for(;now;now--){
if(sta[now]+sta[now-1]<=k)break;
ans++;
}
return sta[now];
}
int main(){
n=gi();k=gi();
for(int i=1;i<n;i++){int u=gi(),v=gi();G[u].push_back(v);G[v].push_back(u);d[u]++;d[v]++;}
int rt=1;
for(int i=2;i<=n;i++)if(d[i]>1){rt=i;break;}
dfs(rt,rt);
printf("%d
",ans+1);
return 0;
}