```cpp
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<vector>
#include<deque>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
//const int M=;
//const int mod=;
void* pointer;
struct Edge{
int u;
int v;
int dis;
}edge[200005];
int n,m;
int father[5005];
inline ll read(){
char ch='\0';
char a;
ll ret=0;
a=getchar();
while(!isalnum(a)){
ch=a;
a=getchar();
}
while(isalnum(a)){
ret*=10;
ret+=a-'0';
a=getchar();
}
if(ch=='-')ret=-ret;
return ret;
}
void init(){
for(int i=1;i<=n;i++){
father[i]=i;
}
return;
}
bool cmp(const Edge& a,const Edge& b){
return a.dis<b.dis;
}
int getfather(int x){
if(x!=father[x]){
father[x]=getfather(father[x]);
}
return father[x];
}
void merge(int x,int y){
father[x]=y;
return;
}
signed main(){
//freopen("","r",stdin);
//freopen("","w",stdout);
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=m;i++){
edge[i].u=read();
edge[i].v=read();
edge[i].dis=read();
}
init();
sort(edge+1,edge+1+m,cmp);
int sum=0;
int cnt=0;
for(int i=1;i<=m;i++){
int f1=getfather(edge[i].u);
int f2=getfather(edge[i].v);
if(f1==f2)continue;
else{
merge(f1,f2);
sum+=edge[i].dis;
cnt++;
if(cnt==n-1)break;
}
}
cout<<sum;
return 0;
}
```
by joker_0 @ 2019-11-11 11:40:02
简单的并查集实现Kruskal
by joker_0 @ 2019-11-11 11:41:11
ios::sync_with_stdio(0)+快读,你不怕爆炸?
by Belarus @ 2019-11-11 11:42:42
@[joker_0](/user/156947)
by Belarus @ 2019-11-11 11:43:15
@[Belarus](/user/223392) Oh,I got it
但是想请教一下为什么会TLE
by joker_0 @ 2019-11-11 11:45:27
@[joker_0](/user/156947) 关闭同步流之后只能cin和cout
by Belarus @ 2019-11-11 11:46:03
那什么getchar啊puts啊都不能用
by Belarus @ 2019-11-11 11:46:25
@[Belarus](/user/223392) 明白了,蟹蟹
by joker_0 @ 2019-11-11 11:47:11
getchar()莫得东西啊
by torque @ 2019-11-11 11:47:16
@[Belarus](/user/223392) 我用得也没出什么问题呢
by Marne @ 2019-11-11 18:29:31