diff --git a/src/P3379/P3379.cpp b/src/P3379/P3379.cpp index 5cd1f6f..b7bbce6 100644 --- a/src/P3379/P3379.cpp +++ b/src/P3379/P3379.cpp @@ -48,6 +48,7 @@ ll lca(ll u,ll v){ } } return fa[u][0]; + } int main(){ diff --git a/src/P5021/P5021.cpp b/src/P5021/P5021.cpp new file mode 100644 index 0000000..ee4bbeb --- /dev/null +++ b/src/P5021/P5021.cpp @@ -0,0 +1,66 @@ +#include + +using ll = int64_t; +using std::cin,std::cout; + +const ll maxn=5e4+5, inf=(ll(1)<<60); +ll n,m,mins{inf},maxs,g[maxn],f[maxn]; +std::vector> adj[maxn]; + +void dfs(ll fth,ll u,const ll &num){ + std::vector vec; + for(auto [v,w]:adj[u]){ + if(fth==v)continue; + dfs(u,v,num); + f[u]+=f[v]; + if(ll ff=g[v]+w;ff>=num){ + ++f[u]; + }else{ + vec.emplace_back(ff); + } + } + std::sort(vec.begin(),vec.end()); + ll l{0},r{ll(vec.size())-1}; + while(l=num){ + f[u]++; + l++,r--; + }else{ + l++; + } + } + if(l==r){ + g[u]=vec[r]; + } +} + +bool check(ll num){ + std::fill(f,f+1+n,0); + std::fill(g,g+1+n,0); + dfs(0,1,num); + return f[1]>=m; +} + +int main(){ + cin>>n>>m; + for(ll i{1};i>u>>v>>w; + mins=std::min(mins,w); + maxs+=w; + adj[u].emplace_back(v,w); + adj[v].emplace_back(u,w); + } + + ll l{mins},r{maxs},ans{l},mid; + while(l<=r){ + mid=(l+r)/2; + if(check(mid)){ + ans=mid; + l=mid+1; + }else{ + r=mid-1; + } + } + cout<