#include<map> #include<set> #include<queue> #include<cmath> #include<cstdio> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define N 300010 #define M 350010 #define lson ch[x][0] #define rson ch[x][1] using namespace std; int cnt[10010],max1,n,m,ans=0x3f3f3f3f; struct node { int from,to,w; } e[N]; int cmp(node a,node b) { return a.w<b.w; } //lct begin int tag[M],f[M],ch[M][2],sum[M]; int not_root(int now) { int x=f[now]; return lson==now||rson==now; } int push_up(int x) if(e[sum[rson]].w>e[sum[x]].w) { sum[x]=sum[rson]; } } int rev(int x) { swap(lson,rson); tag[x]^=1; } int push_down(int x) } int rotate(int x) ch[x][!kd]=y; ch[y][kd]=xs; if(xs) f[xs]=y; f[y]=x; f[x]=z; push_up(y); } int push_all(int x) push_down(x); } int splay(int x) rotate(x); } push_up(x); } int access(int x) { for(int y=0; x; y=x,x=f[x]) { splay(x); rson=y; push_up(x); } } int make_root(int x) { access(x); splay(x); rev(x); } int split(int x,int y) { make_root(x); access(y); splay(y); } int find_root(int x) { access(x); splay(x); while(lson) { push_down(x); x=lson; } return x; } int link(int x,int y) int cut(int x,int y) int print(int x) //lct end //dsu begin int fa[M]; int init() { for(int i=1; i<M; i++) { fa[i]=i; } } int find(int x) int unity(int x,int y) //dsu end int main() { init(); scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w); } sort(e+1,e+m+1,cmp); int tot=0; for(int i=m; i>=1; i--) else { split(e[i].from+m,e[i].to+m); int gg=sum[e[i].to+m]; cut(e[gg].to+m,gg); cut(e[gg].from+m,gg); cnt[e[gg].w]--; cnt[e[i].w]++; while(!cnt[max1]) { max1--; } if(tot==n-1) ans=min(ans,max1-e[i].w); link(e[i].to+m,i); link(e[i].from+m,i); } } printf("%d ",ans); }










