For all edges from large to small sort, enumerate the maximum edges, O (m) Verify, use and check the set maintenance diagram for connectivity.
ProgramCODEVS1001;Constmaxm=5008; MAXN=508; INF=2000000000;typeArr=RecordU,v,w:int64; End;varegArray[0.. MAXM] ofarr; FA:Array[0.. MAXN] ofLongint; I,j,m,n,x,y,z,s,t,u,v,d:longint; Ans1,ans2:int64;procedureAdd (u,v,w:longint);beginInc (J); EG[J].U:=u; EG[J].V:=v; EG[J].W:=W;End; proceduresort (l,r:longint); varI,j,x:longint; Y:arr; beginI:=l; J:=R; X:=eg[(L+r)Div 2].W; Repeat whileEg[i].w>x DoInc (I); whileX>eg[j].w DoDec (j); if not(I>J) Then beginy:=Eg[i]; Eg[i]:=Eg[j]; EG[J]:=y; Inc (I); J:=j-1; End; untilI>J; ifL<j Thensort (l,j); ifI<r Thensort (i,r); End;functionFind (X:longint): Longint;begin ifFa[x]=x Thenexit (x); FA[X]:=find (fa[x]); Exit (Fa[x]);End;functiongcd (x,y:longint): Longint;begin ify=0 ThenExit (x)ElseExit (GCD (y,xMoDy));End;beginreadln (n,m); J:=0; fori:=1 toM Do beginreadln (x, y, z); Add (x, y, z); End; READLN (s,t); Sort (1, M); ANS1:=INF; ANS2:=-INF; fori:=1 toM Do begin forj:=1 toN Dofa[j]:=J; forJ:=i toM Do beginu:=eg[j].u; v:=eg[j].v; X:=find (U); Y:=Find (v); ifX<>y Then beginFa[x]:=y; ifFind (Fa[s]) =find (Fa[t]) Then begin ifEg[i].w*ans2<eg[j].w*ans1 Then beginans1:=EG[I].W; ANS2:=EG[J].W; End; Continue End; End; End; End; D:=gcd (ANS1,ANS2); ifAns1=inf ThenWriteln ('Impossible')Else ifD=ans2 ThenWriteln (ans1DivANS2)ElseWriteln (ans1DivD'/', Ans2Divd);End.
CODEVS1001 Comfortable route (and check set)