RT
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
struct CD{
int to,w;
};
namespace BMT{
vector <CD> tree[500005];
bitset <500005> vist;
struct AB{
int x,y,l;
}st[2000002];
bool operator <(const AB a,const AB b)
{
return a.l<b.l;
}
int n,c,t,fx,fy,fa[2002],tot,x[2002],y[2002];
int findfa(int x)
{
if(fa[x]!=x)
fa[x] = findfa(fa[x]);
return fa[x];
}
bool hebin(int x,int y)
{
fx = findfa(x);
fy = findfa(y);
if(fx!=fy)
{
fa[fx] = fy;
return true;
}
return false;
}
void kruskal()
{
for(int i=1;i<=n;i++)
fa[i] = i;
sort(st+1,st+t+1);
for(int i=1;i<=t;i++)
{
if(hebin(st[i].x,st[i].y))
tree[st[i].x].push_back({st[i].y,st[i].l}),
vist[i] = true,tot++;
if(tot>=n-1)
return ;
}
return ;
}
void buildedge(int x,int y,int z)
{
st[++t] = {x,y,z};
return ;
}
void run()
{
return kruskal();
}
AB unvistmin()
{
for(int i=1;i<=t;i++)
{
if(vist[i]==false)
return {st[i].x,st[i].y,st[i].l};
}
return {st[t].x,st[t].y,st[t].l};
}
}
vector <CD> st[500005];
int n,m,x,y,z,depth[500005],fa[500005][21],mouse[500005][21],maxmouse;
void getfa(int u,int w,int father)
{
depth[u] = depth[father]+1;
fa[u][0] = father;
mouse[u][0] = w;
for(int i=1;i<=20;i++)
fa[u][i] = fa[fa[u][i-1]][i-1],
mouse[u][i] = max(mouse[fa[u][i-1]][i-1],mouse[u][i-1]);
for(int i=0;i<BMT::tree[u].size();i++)
{
int v = BMT::tree[u][i].to,w = BMT::tree[u][i].w;
if(father!=v)
getfa(v,w,u);
}
return ;
}
int lca(BMT::AB it)
{
int u = it.x,v = it.y,w = it.l;
if(depth[u]<depth[v])
swap(u,v);
for(int i=20;i>=0;i--)
if(depth[fa[u][i]]>=depth[v])
{
if(mouse[u][i]<w)
maxmouse = max(mouse[u][i],maxmouse);
u = fa[u][i];
}
if(u==v)
return maxmouse;
for(int i=20;i>=0;i--)
if(fa[u][i]!=fa[v][i])
{
if(mouse[u][i]<w)
maxmouse = max(mouse[u][i],maxmouse);
if(mouse[v][i]<w)
maxmouse = max(mouse[v][i],maxmouse);
u = fa[u][i],v = fa[v][i];
}
return maxmouse;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
BMT::buildedge(x,y,z);
}
BMT::run();
getfa(1,0,0);
printf("%d",lca(BMT::unvistmin()));
return 0;
}