
样例
输入
7 12
1 2 9
1 5 2
1 6 3
2 3 5
2 6 7
3 4 6
3 7 3
4 5 6
4 7 2
5 6 3
5 7 6
6 7 1
输出
16

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct note
{
ll u,v,w;
}e[500005];
int f[200005];
int n,m;
bool cmp(note a,note b)
{
return a.w<b.w;
}
int find(int x)
{
if(f[x]==x) return x;
else return f[x]=find(f[x]);
}
ll Kruskal()
{
ll ans=0;
for(int i=0;i<=n;i++) f[i]=i;
int u,v,x,y;
for(int i=0;i<m;i++)
{
u=e[i].u;
v=e[i].v;
x=find(u);
y=find(v);
if(x!=y)
{
ans+=e[i].w;
f[x]=y;
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++)
cin>>e[i].u>>e[i].v>>e[i].w;
sort(e,e+m,cmp);
cout<<Kruskal()<<endl;
return 0;
}