This commit is contained in:
Zengtudor 2024-11-19 23:47:23 +08:00
parent bdb8bea73f
commit 6edf150bd1
2 changed files with 67 additions and 0 deletions

View File

@ -48,6 +48,7 @@ ll lca(ll u,ll v){
}
}
return fa[u][0];
}
int main(){

66
src/P5021/P5021.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <bits/stdc++.h>
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<std::pair<ll,ll>> adj[maxn];
void dfs(ll fth,ll u,const ll &num){
std::vector<ll> 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<r){
if(vec[l]+vec[r]>=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<n;i++){
ll u,v,w;
cin>>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<<ans<<'\n';
}