可能是题解?
2025-08-06 18:59:08
发布于:上海
1阅读
0回复
0点赞
#include<iostream>//肯定是抄的
#include<algorithm>
using namespace std;
int n,m,b[100010],c[100010],ans=0,cnt=0;
bool flag;
struct z{
int x,y,z;
}a[200010];
bool cmp(z x,z y){
return x.z < y.z;
}
int get(int x){
if(x==b[x])return x;
return b[x]=get(b[x]);
}
void merge(int x,int y){
int fx=get(x),fy=get(y);
if(fx!=fy){
if(c[fx]>c[fy])b[fy]=fx;
else{
b[fx]=fy;
if(c[fx]==c[fy])c[fy]++;
}
}
}
int main(){
cin >> n >> m;
for(int i=1;i<=n;++i)b[i]=i;
for(int i=1,x,y,z;i<=m;i++){
cin >> a[i].x >> a[i].y >> a[i].z;
}
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++){
if(get(a[i].x)!=get(a[i].y)){
merge(a[i].x,a[i].y);
ans += a[i].z;
cnt++;
if(cnt==n-1){
flag=1;
}
}
}
if(flag)cout << ans << end;
else cout << "orz" << end;
return 0;
}
是题解,也许吧.
这里空空如也
有帮助,赞一个