P3366 【模板】最小生成树
#include <bits/stdc++.h>
using namespace std;
const int nn = 5010;
const int mm = 2e5 + 5;
int n,m,fa[nn];
int ans,cnt;
struct node{
int x,y,z;
bool operator <(const node &a){
return z < a.z;
}
}edge[mm];
void Init(int n){
for(int i = 1; i <= n; i++)
fa[i] = i;
}
int find(int x){
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
void kruskual(){
for(int i = 1; i <= m; i++){
int x = find(edge[i].x), y = find(edge[i].y);
if(x == y) continue;
fa[x] = y;
cnt++;
ans+=edge[i].z;
}
}
int main(){
//freopen("in","r",stdin);
ios::sync_with_stdio(0);
cin >> n >> m;
Init(n);
for(int i = 1; i <= m; i++)
cin >> edge[i].x >> edge[i].y >> edge[i].z;
sort(edge + 1,edge + 1 + m);
kruskual();
if(cnt != n - 1) cout << "orz";
else cout << ans;
return 0;
}