ACM 2015 Beijing Online Race F Couple Trees Chairman tree + Tree chain split

Source: Internet
Author: User

Submit

Test instructions: Gave two trees, their heel is 1, then asked, U,v said in the first tree at the U point toward the root node walk, the second tree at the V point to the root node, and then begged them to reach the earliest common point

Solution:

We have the first tree in the book chain, and then the second tree uses the Chairman tree, his information from his father node, each point in his first tree chain after the division of the location, so we every time we query the UV when we select the U and Top[u] This interval in the chairman of the Tree v tree to find, The maximum value that can be taken in this interval, once present, is what we want, and this point preserves the position of all points in the path to the root node after the first tree is split.

#include <iostream>#include<algorithm>#include<cstdio>#include<vector>#include<string.h>using namespacestd;Const intmaxn=100005;Const intmmaxn=100005* -;inth[maxn],to[maxn*2],nx[maxn*2],numofe;intSON[MAXN],DEPTH[MAXN],FA[MAXN],NUM[MAXN],TOP[MAXN];intP[maxn],fp[maxn],pos,sizoftree;intLS[MMAXN],RS[MMAXN],MAV[MMAXN],T[MAXN],DEPTH2[MAXN];voidInitintN) {Sizoftree=pos=numofe=0;  for(intI=0; i<=n; i++) h[i]=0; t[0]=ls[0]=rs[0]=mav[0]=sizoftree=0;}voidAddintUintv) {Numofe++; To[numofe]=v; Nx[numofe]=H[u]; H[u]=Numofe;}voidDfsintCurintPerintDEP) {Depth[cur]=DEP; Son[cur]=-1; Fa[cur]=per; Num[cur]=1;  for(intI=h[cur]; I I=Nx[i]) {        inttt=To[i]; if(Tt==per)Continue; DFS (TT,CUR,DEP+1); Num[cur]+=NUM[TT]; if(son[cur]==-1|| num[Son[cur]]<num[tt]) son[cur]=tt; }}voidFinde (intCurintPerintTP) {Top[cur]=TP; POS++; P[cur]=POS; Fp[pos]=cur; if(son[cur]!=-1) Finde (SON[CUR],CUR,TP);  for(intI=h[cur]; I I=Nx[i]) {        inttt=To[i]; if(tt==per| | Tt==son[cur])Continue;    Finde (TT,CUR,TT); }}voidInsertintLintRintKintPre,int&x) {x=++Sizoftree; LS[X]=Ls[pre]; RS[X]=Rs[pre]; MAV[X]=Max (mav[pre],k); if(L==R)return ; intMid= (l+r) >>1; if(k<=mid) Insert (l,mid,k,ls[pre],ls[x]); ElseInsert (mid+1, r,k,rs[pre],rs[x]);}intCL,CR;intQueryintLintRintroot) {    if(CL&LT;=L&AMP;&AMP;R&LT;=CR)returnMav[root]; if(mav[root]==0)return 0; intMid= (l+r) >>1; inta1=0, a2=0; if(Cl<=mid) a1=query (L,mid,ls[root]); if(Cr>mid) A2=query (mid+1, R,rs[root]); returnMax (A1,A2);}intSolveintUintVintN) {    intfu=Top[u]; intret;  while(true) {CL=p[fu];cr=P[u]; RET=query (1, N,t[v]); if(ret) Break; U=Fa[fu]; Fu=Top[u]; }    returnFp[ret];}intMain () {intn,m;  while(SCANF ("%d%d", &n,&m) = =2) {init (n);  for(intI=2; i<=n; i++)         {             intu; scanf ("%d",&T); Add (I,u);         Add (u,i); } DFS (1,1,1); Finde (1,1,1); depth2[1]=1; Insert (1N1, t[0],t[1]);  for(intI=2; i<=n; i++)         {             intu; scanf ("%d",&T); Depth2[i]=depth2[u]+1; Insert (1, N,p[i],t[u],t[i]); }         intans=0;  for(intI=0; i<m; i++)         {             intu,v; scanf ("%d%d",&u,&v); U= (U+ans)%n+1; V= (V+ans)%n+1; Ans=solve (u,v,n); printf ("%d%d%d\n", ans,depth[u]-depth[ans]+1, depth2[v]-depth2[ans]+1); }    }    return 0;}

ACM 2015 Beijing Online Race F Couple Trees Chairman tree + 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.