@[黄耀风](/user/289275) 我用你的代码,把空间开到5e6,过了
by EDqwq @ 2020-11-24 14:27:46
哦对了,我还加了个路径压缩
by EDqwq @ 2020-11-24 14:28:03
@[林深时x见鹿](/user/294562) 能看看代码吗?
by Terraria @ 2020-11-24 18:32:00
@[黄耀风](/user/289275)
```cpp
#include<bits/stdc++.h>
using namespace std;
struct edge{
int x;
int y;
int val;
}a[500009];
int n,m;
long long ans;
bool hav_ans=true;
int p[500009];
int get_fu(int x){
if(p[x] == x)return x;
else return p[x] = get_fu(p[x]);
}
bool cmp(edge a,edge b){
return a.val<b.val;
}
void kruskal(){
int f1,f2,k=0;
for(int i=1;i<=m;i++){
f1=get_fu(a[i].x);
f2=get_fu(a[i].y);
if(f1!=f2){
ans+=a[i].val;
p[f1]=f2;
k++;
if(k==n-1) break;
}
}
return;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].val;
}
sort(a+1,a+m+1,cmp);
kruskal();
cout<<ans;
}
```
就是我说的改动啊,没啥问题
by EDqwq @ 2020-11-24 18:32:46
@[林深时x见鹿](/user/294562) 啊!我眼瞎了。。。搞了半天原来是数组的问题,开成节点数的大小了。。。谢谢!
by Terraria @ 2020-11-24 18:34:55