HDU-6393 Traffic Network in Numazu (tree chain + base ring tree)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=6393

Test instructions

To the N-point and N-side graphs, there are two operations, one to modify the edge, and the other to query the shortest path from u to v.

Analysis

n points and n edges, is actually a tree + a ring, if it is just a tree, then this problem is the tree chain of the template problem is split.

For the ring, it is possible to consider extracting one edge of the ring first, then the tree chain, modified with a segment tree, single-point modification and interval summation.

When querying, consider three cases, you walk the tree to V, from the edge of the extracted from U and then to V (two kinds), take the minimum value.

As for removing the ring on an edge, use and check the set to engage in.

#include <bits/stdc++.h>#defineMem (A, B) memset (A,b,sizeof (a))using namespaceStd;typedefLong Longll;Const intINF =0x3f3f3f3f;Const intMAXN = 2e5+5;structSide {intu,v,w; intNE;} S[MAXN];structEdge {intL,r; ll v;} E[MAXN<<2];intn,q;intHead[maxn],len;intVAL[MAXN],HOLD[MAXN],PRE[MAXN];intDeep[maxn],fa[maxn],ve[maxn],son[maxn],top[maxn],p[maxn],fp[maxn],sz;voidAddintUintVintW) {s[len].u=u; S[LEN].V=v; S[LEN].W=W; S[len].ne=Head[u]; Head[u]= len++;}intFindintx) {returnPRE[X] = = X?x:pre[x] =find (Pre[x]);}voidDFS1 (intUintPintd) {Deep[u]=D; Fa[u]=p; Ve[u]=1; Son[u]= -1;  for(inti = Head[u]; I!=-1; i =s[i].ne) {        if(s[i].v = = p)Continue; VAL[S[I].V]= S[I].W;//assigning edge weights to connected pointsHold[i>>1] = S[I].V;//at which point the weight of the edge is held .DFS1 (s[i].v,u,d+1); Ve[u]+=VE[S[I].V]; if(Son[u] = =-1|| Ve[s[i].v]>Ve[son[u]]) Son[u]=s[i].v; }    return ;}voidDFS2 (intUintsp) {Top[u]=sp; P[u]= ++sz; Fp[p[u]]=u; if(Son[u] = =-1)return ;    DFS2 (SON[U],SP);  for(inti = Head[u]; I!=-1; i =s[i].ne) {        if(s[i].v = = son[u]| | S[I].V = = Fa[u])Continue;    DFS2 (S[I].V,S[I].V); }    return ;}voidBuildintIintLintr) {E[I].L=l; E[I].R=R; if(L = =r) {e[i].v=Val[fp[l]]; return ; }    intMid = (l+r) >>1; Build (I<<1, L,mid); Build (I<<1|1, mid+1, R); E[I].V= e[i<<1].v+e[i<<1|1].v;}voidModifyintIintPosintv) {if(Pos> e[i].r| | pos< E[I].L)return ; if(E[I].L = =E[I].R) {E[I].V=v; return ; } Modify (I<<1, pos,v); Modify (I<<1|1, pos,v); E[I].V= e[i<<1].v+e[i<<1|1].V;} ll query (intIintLintr) {if(e[i].r< l| | E[i].l> R)return 0; if(e[i].l>= l&&e[i].r<= R)returne[i].v; returnQuery (i<<1, L,r) +query (i<<1|1, l,r);} ll demand (intXinty) {intFX =Top[x]; intFY =Top[y]; ll ans=0;  while(fx!=FY) {        if(deep[fx]<Deep[fy])            {swap (FX,FY);        Swap (x, y); } ans+ = Query (1, p[fx],p[x]); X=FA[FX]; FX=Top[x]; }    if(x = = y)returnans; if(deep[x]>Deep[y]) swap (x, y); Ans+ = Query (1, P[son[x]],p[y]); returnans;}voidinit () {sz= Len =0; Mem (Head,-1);  for(inti =0; i<= N; i++) Pre[i] =i;}intMain () {intT; CIN>>T;  while(t--) {        intSu,sv,sc,ss; scanf ("%d%d",&n,&q);        Init ();  for(inti =1; i<= N; i++) {            intu,v,w; scanf (" %d%d%d",&u,&v,&W); intFX =find (U); intFY =Find (v); if(FX = =FY) {Len+=2; SS=i; Su=u; SV=v; SC=W; Continue; } ElsePre[fy]=FX;            Add (U,V,W);        Add (V,U,W); } DFS1 (1,-1,1); DFS2 (1,1); Build (1,1, N);  while(q--) {            intO,x,y; scanf (" %d%d%d",&o,&x,&y); if(O = =0) {x--; if(x = =SS) {SC=y; Continue; } Modify (1, P[hold[x]],y); } Else{ll ans; Ans= Sc+min (Demand (X,SU) +demand (Y,SV), demand (X,SV) +demand (Y,SU)); Ans=min (Ans,demand (x, y)); printf ("%lld\n", ans); }        }    }    return 0;}

HDU-6393 Traffic Network in Numazu (tree chain + base ring tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.