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