Tree Chain Split

Source: Internet
Author: User

http://blog.sina.com.cn/s/blog_7a1746820100wp67.html This blog is very good, very clear.

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13013 This is a simple question.

The code is as follows:

#include <queue>#include<stack>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineINF 99999999#definell __int64#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMAXN =10010;structnode{intto ; intv; intNext;} EDGE[MAXN*3];intPre[maxn],ind,top[maxn],fa[maxn],son[maxn],w[maxn],deq[maxn],siz[maxn],fn;inttree[maxn<<2],val[maxn][3],n;voidAddintXintYintz) {edge[ind].to=y; EDGE[IND].V=Z; Edge[ind].next=Pre[x]; PRE[X]= ind++;}//first search find siz[],son[],deq[],fa[]voidDFS1 (intRtintPaintd) {Deq[rt]=D; SON[RT]=0; FA[RT]=PA; SIZ[RT]=1; inti;  for(I=pre[rt]; i!=-1; I=Edge[i].next) {        intt =edge[i].to; if(t! =Fa[rt]) {DFS1 (t,rt,d+1); SIZ[RT]+=Siz[t]; if(Siz[son[rt]] < siz[t]) {//if the parent node's SON[RT] iz[] is less than the child node's siz[] Update son[]SON[RT] =T; }        }    }}//second search find w[],top[]voidDFS2 (intRtintTP) {Top[rt]=TP; W[RT]= ++N; if(Son[rt]! =0) DFS2 (SON[RT],TP);//if the current parent node is pure in Son, then in order to let the chain in the line segment tree to search for son[] consecutively, and the top of the edge of the same point.      for(intI=PRE[RT]; i!=-1; I=Edge[i].next) {        intt =edge[i].to; if(t! = Fa[rt] && son[rt]! =t)        {DFS2 (t,t); }    }}voidUpdata (intPintVintLintRintRT) {    if(L = =R) {Tree[rt]=v; return ; }    intm = (l+r)/2; if(M >=p)    {Updata (P,v,lson); }    Else{updata (P,v,rson); } Tree[rt]= Max (tree[rt<<1],tree[rt<<1|1]);}intQueryintLintRintLintRintRT) {    if(L<=l && r<=s) {        returnTree[rt]; }    intm = (l+r)/2; intAns =0; if(M >=L) {ans=Max (Ans,query (L,r,lson)); }    if(M <R) {ans=Max (Ans,query (L,r,rson)); }    returnans;}intLcaintXinty) {    intF1,f2; intAns =0;  while(Top[x]! =Top[y]) {        if(Deq[top[x]) <Deq[top[y]])        {Swap (x, y); } ans= Max (Ans,query (w[top[x]],w[x),1Fn1)); X=Fa[top[x]]; }    if(x = =y)returnans; if(Deq[x] <Deq[y])    {Swap (x, y); }    returnMax (Ans,query (w[son[y]],w[x),1Fn1))    ;}intMain () {inti,j,t; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); IND=1; memset (PRE,-1,sizeof(pre));  for(i=1; i<n; i++){            intx, Y, Z scanf ("%d%d%d",&x,&y,&z); val[i][0] =x; val[i][1] =y; val[i][2] =Z;            Add (x, y, z);        Add (y,x,z); } memset (Tree,0,sizeof(tree)); DFS1 (1,1,1); FN=0; DFS2 (1,1);  for(i=1; i<n; i++){            if(deq[val[i][0]] > deq[val[i][1]]) {Swap (val[i][0],val[i][1]); } updata (w[val[i][1]],val[i][2],1Fn1); }        Chars[ the];  while(1) {scanf ("%s", s); if(s[0] =='D')                 Break; if(s[0] =='C'){                intx, y; scanf ("%d%d",&x,&y); Updata (w[val[x][1]],y,1Fn1); }            Else {                intx, y; scanf ("%d%d",&x,&y); printf ("%d\n", LCA (x, y)); }        }    }}

Tree Chain Split

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.