kruscal
作者:互联网
include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int fi[maxn],ne[maxn],v[maxn],w[maxn];
struct re
{
int x,y,z;
};
re a[maxn];
int fa[maxn];
int t=-1;
bool cmp(re a,re b)
{
return a.z<b.z;
}
int find(int k)
{
if (k==fa[k]) return k;
else return find(fa[k]);
}
int main()
{
int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
}
sort(a+1,a+1+m,cmp);
int ans=0;
int mark=0;
for (int i=1;i<=m;i++)
{
if (find(a[i].x)==find(a[i].y)) continue;
ans+=a[i].z;
fa[find(a[i].x)]=find(a[i].y);
mark++;
if (mark==n-1) break;
}
if (mark!=n-1) cout<<"orz";else cout<<ans;
}
标签:fa,int,mark,re,kruscal,maxn,find 来源: https://www.cnblogs.com/reshuffle/p/12344994.html