diff --git a/src/P4186/P4186.cpp b/src/P4186/P4186.cpp new file mode 100644 index 0000000..b9802b2 --- /dev/null +++ b/src/P4186/P4186.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include + +using ll = int64_t; +using std::cin, std::cout; + +const ll maxn = 1e5+5; +ll n, k, dpts[maxn], rds[maxn], ans; +std::vector 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::max()); + + for(ll i{1};i>u>>v; + edgs[u].push_back(v); + edgs[v].push_back(u); + } + initdepth(0, k); + initrd(0, k); + dfs(0, k); + cout<