1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 const int MAXN=300001;
7 struct node
8 {
9 int u;
10 int v;
11 int w;
12 }edge[MAXN];
13 int num=1;
14 int father[MAXN];
15 int comp(const node & a,const node & b)
16 {
17 if(a.w<b.w)return 1;
18 else return 0;
19 }
20 int find(int x)
21 {
22 if(father[x]!=x)
23 father[x]=find(father[x]);
24 return father[x];
25 }
26 void unionn(int x,int y)
27 {
28 int fx=find(x);
29 int fy=find(y);
30 father[fx]=fy;
31 }
32 int main()
33 {
34 int n,m;
35 scanf("%d%d",&n,&m);
36 for(int i=1;i<=n;i++)father[i]=i;
37 for(int i=1;i<=m;i++)
38 {
39 scanf("%d%d%d",&edge[num].u,&edge[num].v,&edge[num].w);
40 num++;
41 }
42 sort(edge+1,edge+num,comp);
43 long long int k=0;
44 long long int tot=0;
45 for(int i=1;i<=num-1;i++)
46 {
47 if(find(edge[i].u)!=find(edge[i].v))
48 {
49 unionn(edge[i].u,edge[i].v);
50 tot=tot+edge[i].w;
51 k++;
52 }
53 if(k==n-1)break;
54 }
55 printf("%lld",tot);
56 return 0;
57 }