3732:network
Title: Portal
Exercises
At first glance, the largest edge is the smallest, and the direct wave is the smallest spanning tree.
At first, I was afraid it would be wrong to ask a wave of meat, any two points the maximum path on the minimum spanning tree must be minimal.
Then things are going to be easier. Hey, build a tree, direct online LCA, with a mx[i][j] record the maximum value of this interval for the 2^j.
Code:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <cmath>5#include <algorithm>6 using namespacestd;7 structnode8 {9 intx,y,c;Ten}a[111000];intLen; One voidInsintXintYintc) A { -Len++;a[len].x=x;a[len].y=y;a[len].c=C; - } the structEdge - { - intX,y,c,next; -}e[111000];intllen,last[15100]; + voidInssintXintYintc) - { +Llen++;e[llen].x=x;e[llen].y=y;e[llen].c=C; Ae[llen].next=last[x];last[x]=Llen; at } - inteax15100]; - intFINDFA (intx) {if(fa[x]!=x) Fa[x]=findfa (fa[x]);returnfa[x];} - BOOLCMP (node N1,node n2) {returnn1.c<n2.c;} - intn,m,t; - intbin[ +],dep[15100],f[15100][ +]; in intmx[15100][ +];//record the maximum edge value of a i~i+2^j - voidPre_tree_node (intx) to { + for(intI=1; i<= -&& dep[x]>=bin[i];i++) -f[x][i]=f[f[x][i-1]][i-1],mx[x][i]=max (mx[x][i-1],mx[f[x][i-1]][i-1]); the for(intk=last[x];k;k=e[k].next) * { $ inty=e[k].y;Panax Notoginseng if(y!=f[x][0]) - { thedep[y]=dep[x]+1; +f[y][0]=x;mx[y][0]=e[k].c; A Pre_tree_node (y); the } + } - } $ intSolintXinty) $ { - intmaxx=0; - if(dep[x]<Dep[y]) swap (x, y); the for(intI= -; i>=0; i--) - if(dep[x]-dep[y]>=Bin[i])WuyiMaxx=max (Maxx,mx[x][i]), x=F[x][i]; the if(x==y)returnMaxx; - for(intI= -; i>=0; i--) Wu if(Dep[x]>=bin[i] && f[x][i]!=F[y][i]) -Maxx=max (Maxx,max (mx[x][i],mx[y][i)), x=f[x][i],y=F[y][i]; AboutMaxx=max (Maxx,max (mx[x][0],mx[y][0])); $ returnMaxx; - } - intMain () - { Abin[0]=1; for(intI=1; i<= -; i++) bin[i]=bin[i-1]<<1; +scanf"%d%d%d",&n,&m,&T); thellen=len=0; Memset (Last,0,sizeof(last)); - for(intI=1; i<=m;i++) $ { the intX,Y,C;SCANF ("%d%d%d",&x,&y,&c); the ins (x,y,c); ins (y,x,c); the } theSort (A +1, a+len+1, CMP); - for(intI=1; i<=n;i++) fa[i]=i;inttt=0; in for(intI=1; i<=len;i++) the { the intFX=FINDFA (a[i].x), fy=Findfa (A[I].Y); About if(fx!=fy) the { thefa[fy]=fx,tt++; the INSS (A[I].X,A[I].Y,A[I].C); + INSS (A[I].Y,A[I].X,A[I].C); - if(tt==n-1) Break; the }Bayi } the for(intI=1; i<=n;i++) the if(fa[i]==i) -f[i][0]=0, dep[i]=1, Pre_tree_node (i); - while(t--) the { the intST,ED;SCANF ("%d%d",&st,&ed); theprintf"%d\n", Sol (st,ed)); the } - return 0; the}
Bzoj3732:network (minimum spanning tree +lca)