WA了3个点,代码求调
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int f = 1,x = 0; char ch = getchar();
while(ch > '9' || ch < '0'){if(ch == '-')f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){x=(x << 1)+(x << 3)+(ch ^ 48); ch = getchar();}
return f*x;
}
inline void write(int x)
{
if(x < 0){putchar('-'); x = -x;}
if(x > 9)write(x / 10); putchar((x % 10) ^ 48);
return;
}
const int N=1e5+10,M=6e5+10;
struct ed{
int nex,to,val;
}e[M];
struct ed2{
int u,v,val;
bool vis;
}e2[M];
int tot,head[N],n,m;
//Kruskal
int fa[N];
inline bool cmp(ed2 x,ed2 y){
return x.val<y.val;
}
inline int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
//预处理
int dep[N],f[N][32];//f:x的2^k祖先
long long val1,val2,g[N][32][2],ans;//g:从x到f(x,k)的路径的最大边权和严格次大边权
void bfs(int root){
dep[root]=0;
queue<int>q;q.push(root);
while(!q.empty()){
int x=q.front(),len=(int)log2(dep[x]+1);
q.pop();
for(int i=head[x];i;i=e[i].nex){
int y=e[i].to;
if(y==f[x][0]) continue;
dep[y]=dep[x]+1;
f[y][0]=x,g[y][0][0]=e[i].val,g[y][0][1]=LONG_LONG_MIN;
q.push(y);
for(int k=1;k<=len;k++){
f[y][k]=f[f[y][k-1]][k-1];
if(g[y][k-1][0]!=g[f[y][k-1]][k-1][0]){
g[y][k][0]=max(g[y][k-1][0],g[f[y][k-1]][k-1][0]);
g[y][k][1]=min(g[y][k-1][0],g[f[y][k-1]][k-1][0]);
}else{
g[y][k][0]=g[y][k-1][0];
g[y][k][1]=max(g[y][k-1][1],g[f[y][k-1]][k-1][1]);
}
}
}
}
}
inline void upp(int x){
if(x>val1) val2=val1,val1=x;
else if(x>val2 && x!=val1) val2=x;
}
inline void update(int x,int k){
upp(g[x][k][0]);upp(g[x][k][1]);
}
inline void lca(int x,int y){
val1=val2=LONG_LONG_MIN;
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]>dep[y]){
int k=(int)log2(dep[x]-dep[y]);
update(x,k);x=f[x][k];
}
if(x==y) return;
for(int k=(int)log2(dep[x]);k>=0;k--){
if(f[x][k]==f[y][k]) continue;
update(x,k);update(y,k);
x=f[x][k],y=f[y][k];
}
update(x,0);update(y,0);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
e2[i].u=read(),e2[i].v=read(),e2[i].val=read();
for(int i=1;i<=n;i++) fa[i]=i;
sort(e2+1,e2+m+1,cmp);
for(int i=1;i<=m;i++){
int a=find(e2[i].u),b=find(e2[i].v);
if(a==b) continue;
e2[i].vis=1,fa[a]=b,ans+=e2[i].val;
e[++tot].to=e2[i].v,e[tot].nex=head[e2[i].u],e[tot].val=e2[i].val,head[e2[i].u]=tot;
e[++tot].to=e2[i].u,e[tot].nex=head[e2[i].v],e[tot].val=e2[i].val,head[e2[i].v]=tot;
}
bfs(1);
long long ans1=LONG_LONG_MAX;
for(int i=1;i<=m;i++){
if(e2[i].vis) continue;
lca(e2[i].u,e2[i].v);
if(val1!=e2[i].val) ans1=min(ans1,ans-val1+e2[i].val);
else ans1=min(ans1,ans-val2+e2[i].val);
}
printf("%lld\n",ans1);
return 0;
}