样例也过不了,感觉是lca写错了```cpp #include <bits/stdc++.h> using namespace std; const int N=2e5+5; int n,m; struct edge{ int u,v,w; }; bool cmp(edge x,edge y){ return x.w<y.w; } vectorvc; int b[N]; int gtf(int x){ if(b[x]==x)return x; b[x]=gtf(b[x]); return b[x]; } vector<pair<int,int> >rvc[N]; int f[N][21],depth[N],vis[N],fw[N][20]; void init(int u,int fa){ f[u][0]=fa; for(int i=1;i<=20;i++){ f[u][i]=f[f[u][i-1]][i-1]; fw[u][i]=min(fw[u][i-1],fw[f[u][i-1]][i-1]); } for(pair<int,int>p:rvc[u]){ if(p.second==fa)continue; depth[p.second]=depth[u]+1; fw[p.second][0]=p.first; init(p.second,u); } } int find(int x,int y){ if(depth[x]<depth[y])swap(x,y); int ans=5e5; for(int i=20;i>=0;i--){ if(depth[f[x][i]]<=depth[y]){ ans=min(ans,fw[x][i]); x=f[x][i]; } } if(x==y)return ans; for(int i=20;i>=0;i--){ if(f[x][i]!=f[y][i]){ ans=min(ans,fw[x][i]); ans=min(ans,fw[y][i]); x=f[x][i]; y=f[y][i]; } } ans=min(ans,fw[x][0]); ans=min(ans,fw[y][0]); return ans; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ b[i]=i; } for(int i=1;i<=m;i++){ edge e; cin>>e.u>>e.v>>e.w; vc.push_back(e); } sort(vc.begin(),vc.end(),cmp); int ans1=0,cnt=0; int i=0; for(int i=0;i<m;i++){ edge e=vc[i]; int f1=gtf(e.u),f2=gtf(e.v); if(f1==f2)continue; ans1+=e.w; rvc[e.u].push_back(make_pair(e.w,e.v)); rvc[e.v].push_back(make_pair(e.w,e.u)); b[f1]=f2; vis[i]=1; } init(1,0); // for(int i=1;i<=n;i++){ // for(int j=1;j<=3;j++){ // cout<<fw[i][j]<<' '; // } // cout<<endl; // } int ans=5e5; for(int i=0;i<m;i++){ if(vis[i])continue; int t=find(vc[i].u,vc[i].v); if(t==vc[i].w)continue; ans=min(ans,vc[i].w-t);
}
// cout<<ans1<<endl; cout<<ans+ans1; return 0; }