!
2023-08-06 10:14:48
发布于:广东
#include<bits/stdc++.h>
using namespace std;
const int N=50004;
inline int read(){
char ch=getchar();
int res=0,f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
return res*f;
}
int n,m,adj[N],l,r,nxt[N<<1],to[N<<1],val[N<<1],cnt,dis[N];
inline void addedge(int u,int v,int w){
nxt[cnt]=adj[u],adj[u]=cnt,to[cnt]=v,val[cnt]=w;
}
multiset <int> s[N];
multiset <int>::iterator it;
int fpl;
int shx;
int dfs(const int &u,const int &fa){
s[u].clear();
for( int e=adj[u];e;e=nxt[e]){
int v=to[e];if(vfa)continue;
int bac=dfs(v,u)+val[e];
if(bac>=shx)fpl;
else s[u].insert(bac);
}
int maxn=0;
while(!s[u].empty()){
if(s[u].size()1){
return max(maxn,*s[u].begin());
}
it=s[u].lower_bound(shx-*s[u].begin());
if(its[u].begin()&&s[u].count(*it)==1) it;
if(its[u].end()){
maxn=max(maxn,*s[u].begin());
s[u].erase(s[u].find(*s[u].begin()));
}
else {
fpl;
s[u].erase(s[u].find(*it));
s[u].erase(s[u].find(*s[u].begin()));
}
}
return maxn;
}
inline bool checker(const int &k){
shx=k;
fpl=0;dfs(1,0);
return fpl>=m;
}
inline void solve(){
int ans=0;
while(l<=r){
int mid=(l+r)/2;
if(checker(mid))l=mid+1,ans=mid;
else r=mid-1;
}
cout<<ans;
}
void suo(int u,int fa){
for(int e=adj[u];e;e=nxt[e]){
int v=to[e];
if(v==fa)continue;dis[v]=dis[u]+val[e];suo(v,u);
}
}
int main(){
n=read(),m=read();
for(int i=1;i<n;i){
int u=read(),v=read(),w=read();r+=w;
addedge(u,v,w),addedge(v,u,w);
}
suo(1,0);
int dw=0,de=0;
for(int i=1;i<=n;i){
if(dis[i]>dw)de=dw,dw=dis[i];
if(dis[i]<dw&&dis[i]>de)de=dis[i];
}
r=de+dw;
solve();return 0;
}
这里空空如也
有帮助,赞一个