做一个伪最小生成树,求出最小的边权。
这里推荐用kruskal。
\(Code:\)
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
template<class T>void r(T &a)
{
T s=0,w=1;a=0;char ch=getc(stdin);
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getc(stdin);}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getc(stdin);}
a=w*s;
}
template<class T,class... Y>void r(T& t,Y&... a){r(t);r(a...);}
struct edge
{
int u,v,w;
};
struct bcj
{
int father[10010];
void start(int n)
{for(int i=1;i<=n;i++)father[i]=i;}
int find(int x)
{if(father[x]!=x)father[x]=find(father[x]);return father[x];}
void unionn(int x,int y)
{x=find(x);y=find(y);if(x!=y)father[y]=x;}
bool judge(int x,int y)
{if(find(x)==find(y))return true;return false;}
};
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int main()
{
bcj uf;
edge e[20010];
int n,m,ans=0;
r(n,m);
uf.start(n);
for(int i=1;i<=m;i++)
r(e[i].u,e[i].v,e[i].w);
sort(e+1,e+m+1,cmp);
for(int i=1;i<=m;i++)
{
int x=e[i].u,y=e[i].v;
if(!uf.judge(x,y))
{
uf.unionn(x,y);
ans=max(ans,e[i].w);
}
}
cout<<ans;
return 0;
}