代码如下
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100005;
int n;
struct EDGE{
int u,v,w;
}edge[N];
int Arry[N];
int cmp(EDGE a,EDGE b){
return a.w<b.w;
}
void init(){
for(int i = 0; i < n; i++){
Arry[i] = i;
}
}
int Find(int num){
int root = num,tmp;
while(Arry[root]!=root){
root = Arry[root];
}
while(num != root){
tmp = Arry[num];
Arry[num] = root;
num = tmp;
}
return num;
}
bool Union(int a,int b){
int aRoot = Find(a);
int bRoot = Find(b);
if(aRoot != bRoot){
Arry[aRoot] = bRoot;
return true;
}
return false;
}
int main()
{
int m,root,u,v,w,ans = 0,num = 0;
scanf("%d%d%d", &n, &m, &root);
for(int i = 0; i < m; i++){
scanf("%d%d%d", &edge[i].u,&edge[i].v,&edge[i].w);
}
sort(edge, edge+m, cmp);
init();
for(int i = 0; i < m; i++){
if(Union(edge[i].u,edge[i].v)){
ans = edge[i].w;
if(++num == n-1) break;
}
}
printf("%d\n", ans);
return 0;
}