传送门
按时间排序,判断联通块的个数即可
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int fat[1100];
int find(int x)
{
if(fat[x]==x) return x;
return fat[x]=find(fat[x]);
}
struct node{
int x,y;
int time;
}a[110000];
bool comp(const node&a,const node&b)
{
return a.time<b.time;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fat[i]=i;
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].time);
sort(a+1,a+m+1,comp);
int tot=n;
int i;
for(i=1;i<=m;i++)
{
int f1=find(a[i].x),f2=find(a[i].y);
if(f1!=f2)
fat[f1]=f2,tot--;
if(tot==1)
break;
}
if(tot==1)
printf("%d",a[i].time);
else
printf("-1");
return 0;
}