tema
Ejemplo
Entrada
4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2
Salida
4
Nota
Ideas
Preguntas de revisión
De acuerdo con el significado de la pregunta, busque un árbol de expansión donde el peso máximo del borde de todos los bordes sea el más pequeño. Según la naturaleza del árbol de expansión mínimo es el árbol de expansión de cuello de botella, se transforma en el problema de encontrar el árbol de expansión mínimo y el borde más grande se guarda en ans.
Código
#include<iostream>
#include<algorithm>
using namespace std;
const int N=50005;
const int M=100005;
struct edge
{
int u,v,w;
bool operator<(const edge &t) const
{
return w<t.w;
}
}e[M];
int n,m,rt;
int f[N];
void init(int n)
{
for(int i=1;i<=n;i++)
f[i]=i;
}
int find(int x)
{
if(f[x]==x)
return x;
return f[x]=find(f[x]);
}
bool unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return false;
f[x]=y;
return true;
}
int kruskal_return_max()
{
sort(e,e+m);
int cnt=0,ans=0;
for(int i=0;i<m;i++)
{
if(unite(e[i].u,e[i].v))
{
ans=max(ans,e[i].w);
if(++cnt==n-1)
break;
}
}
return cnt==n-1?ans:-1;
}
int main()
{
scanf("%d%d%d",&n,&m,&rt);
init(n);
for(int i=0;i<m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
printf("%d\n",kruskal_return_max());
return 0;
}