链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083
思路:连接所有点,肯定最少是需要n-1条边的,也就是写个最小生成树,记得保存下最大的权值就好了
实现代码:
#include<bits/stdc++.h>
using namespace std; const int M = 3e4+;
int f[M],cnt; struct node{
int u,v,w;
}e[]; bool cmp(node a,node b){
return a.w < b.w;
} int Find(int x){
if(x == f[x]) return x;
return f[x] = Find(f[x]);
} int main()
{
int ans= ,n, m;
cin>>n>>m;
for(int i = ;i <= m;i ++){
cin>>e[i].u>>e[i].v>>e[i].w;
}
sort(e+,e++m,cmp);
for(int i = ;i <= n;i ++) f[i] = i;
for(int i = ;i <= m;i ++){
int fx = Find(e[i].u),fy = Find(e[i].v);
if(fx != fy){
f[fy] = fx;
ans = e[i].w;
}
}
cout<<n-<<" "<<ans<<endl;
}