看!题面被吃掉了!https://www.luogu.org/problemnew/show/P1111
按时间sort一遍,每次合并两个节点,显然如果原先不连通那么合并之后联通块数量–
然后如果n==1就输出当前时间return
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1005;
int n,m,uu,vv,tt,fu,fv,f[N],ans;
struct cyq{
int u,v,t;
}a[100005];
bool cmp(cyq aa,cyq bb){
return aa.t<bb.t;
}
int find(int h)
{
if(f[h]==h) return h;
f[h]=find(f[h]);
return f[h];
}
inline int get(){
char cc=getchar();
int res=0;
while (cc<'0'||cc>'9') cc=getchar();
while (cc>='0'&&cc<='9'){
res=(res<<3)+(res<<1)+cc-'0';
cc=getchar();
}
return res;
}
void work(int k){
uu=a[k].u;vv=a[k].v;tt=a[k].t;
fu=find(uu);fv=find(vv);
if(fu==fv) return;
f[fu]=fv;ans=a[k].t;
}
bool check()
{
int ff=find(1);
for(int i=2;i<=n;i++)
if(find(i)!=ff)
return false;
return true;
}
int main()
{
n=get();m=get();
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++) {a[i].u=get();a[i].v=get();a[i].t=get();}
sort(a+1,a+1+m,cmp);
for(int i=1;i<=m;i++) work(i);
if(check()==false) printf("-1\n");
else printf("%d\n",ans);
return 0;
}