This commit is contained in:
Zengtudor 2024-11-17 10:30:24 +08:00
parent c22aa52d7b
commit f737de093d

62
src/P4186/P4186.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <limits>
#include <vector>
using ll = int64_t;
using std::cin, std::cout;
const ll maxn = 1e5+5;
ll n, k, dpts[maxn], rds[maxn], ans;
std::vector<ll> edgs[maxn];
void initdepth(const ll &fth, const ll &now){
dpts[now]=dpts[fth]+1;
for(const ll &nxt:edgs[now]){
if(nxt==fth)continue;
initdepth(now, nxt);
}
}
void initrd(const ll &fth, const ll &now){
if(edgs[now].size()==1)rds[now]=1;
for(const ll &nxt:edgs[now]){
if(nxt==fth)continue;
initrd(now, nxt);
rds[now]=std::min(rds[now], rds[nxt]+1);
}
}
void dfs(const ll &fth, const ll &now){
if(rds[now]<=dpts[now]){
++ans;
return;
}
if(edgs[now].size()==1){
++ans;
return;
}
for(const ll &nxt: edgs[now]){
if(nxt==fth)continue;
dfs(now, nxt);
}
}
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>k;
std::fill(rds, rds+1+n, std::numeric_limits<unsigned int>::max());
for(ll i{1};i<n;i++){
ll u, v;
cin>>u>>v;
edgs[u].push_back(v);
edgs[v].push_back(u);
}
initdepth(0, k);
initrd(0, k);
dfs(0, k);
cout<<ans<<'\n';
}