@[tabuloforme](/user/399402) 数据有重边,运行时一直执行s++会爆栈。
改成这样即可AC。
```cpp
#include<bits/stdc++.h>
using namespace std;
int f[200005],n,m;
long long sum=0;
struct M{
int x,y,z;
}a[200005];
int cmp(M a,M b){
return a.z<b.z;
}
int find(int a){
if(f[a]!=a)f[a]=find(f[a]);
return f[a];
}
void unionn(int a,int b){
a=find(a);
b=find(b);
f[b]=a;
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++)cin>>a[i].x>>a[i].y>>a[i].z;
for(int i=1;i<=n;i++)f[i]=i;
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++){
if(find(a[i].x)!=find(a[i].y)){
unionn(a[i].x,a[i].y);
sum+=a[i].z;
}
}
int p=find(1);
for(int j=2;j<=n;j++){
int q=find(j);
if(p!=q){
cout<<"orz";
return 0;
}
}
cout<<sum;
return 0;
}
```
by ReTF @ 2022-08-11 10:56:09
@[tabuloforme](/user/399402)
```
else
s++;
```
应该是这个炸了
为什么不能写成 `i<=m`然后加个计数器判断连边到达`n-1`跳出呢
by iiiiiyang @ 2022-08-11 10:59:01
谢谢大佬!
此贴完结
by tabuloforme @ 2022-08-11 11:00:20