kruskal求最小生成树,先把边按边权排序,然后从小到大,每次合并两个节点,如果不在一连联通块上,那么这条边就是我们要统计的边,用并查集维护,总复杂度O(ElogE)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define pa pair<int,int>
#define N 310
#define M 10010
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,fa[N],ans=0;
struct edge{
int x,y,val;
}data[M];
inline bool cmp(edge x,edge y){return x.val<y.val;}
inline int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
inline void merge(int x,int y,int val){
int xx=find(x),yy=find(y);
if(xx==yy) return;
fa[xx]=yy;ans=max(ans,val);
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1;i<=m;++i) data[i].x=read(),data[i].y=read(),data[i].val=read();
sort(data+1,data+m+1,cmp);
for(int i=1;i<=m;++i) merge(data[i].x,data[i].y,data[i].val);
printf("%d %d",n-1,ans);
return 0;
}