Hdu 6203 Ping Ping Ping (greedy + tree-like array +dfs order)

Source: Internet
Author: User

Topic Link: hdu 6203 ping Ping Ping

Test instructions

Give you a tree n+1 tree, there is now a Q-path does not pass, ask you at least how many nodes broken down.

Exercises

Consider the greedy, the Q path to the LCA, according to the depth of the LCA from large to small sort.

Then for each path, see if x or Y is in a subtree that has been banned, and if not, then ans++, mark the subtrees Tree of LCA (x, y) as forbidden.

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i= (a); i<= (b); ++i)3 #defineMST (A, B) memset (A,b,sizeof (a))4 using namespacestd;5 6 Const intn=1e4+7;7 intT,n,m,sum[n];8 intnxt[2*n],g[n],v[2*n],ed,x,y;9 intHs[n],fa[n],top[n],dep[n],sz[n],dfsl[n],dfsr[n],idx;Ten  One voidAdgintXintY) {v[++ed]=y,nxt[ed]=g[x],g[x]=Ed;}  A  - voidDFS1 (intUintpre) { -dfsl[u]=++idx,dep[u]=dep[pre]+1, hs[u]=0, fa[u]=pre,sz[u]=1; the      for(intI=g[u];i;i=nxt[i])if(v[i]!=pre) -DFS1 (V[i],u), sz[u]+=sz[v[i]],hs[u]= (Sz[v[i]]>sz[hs[u])?V[i]:hs[u]; -dfsr[u]=idx; - } + voidDFS2 (intUintTP) { -top[u]=TP; +     if(Hs[u]) DFS2 (HS[U],TP); A      for(intI=g[u];i;i=Nxt[i]) at     if(v[i]!=fa[u]&&v[i]!=Hs[u]) DFS2 (V[i],v[i]); - } -  - intLCA (intXinty) - { -      while(top[x]!=Top[y]) indep[top[x]]>dep[top[y]]?x=fa[top[x]]:y=Fa[top[y]]; -     returnDep[x]<dep[y]?x:y; to } +  -InlinevoidAddintXintc) { while(x<=n) sum[x]+=c,x+=x&-x;} theInlineintAskintx) {intan=0; while(x) an+=sum[x],x-=x&-x;returnan ;} *  $ structNodePanax Notoginseng { -     intX,y,lca,val; theNode (intA=0,intb=0,intC=0,intD=0): X (a), Y (b), LCA (c), Val (d) {} +     BOOL operator< (ConstNode &b)Const{returnVal>B.val;} A}q[n*5]; the  + intMain () { -      while(~SCANF ("%d",&N)) $     { $MST (G,0), MST (SUM,0), ed=0; -F (I,1, N) -         { thescanf"%d%d",&x,&y); -x++,y++;Wuyi ADG (x, y), ADG (y,x); the         } -n++,idx=0, DFS1 (1,0), DFS2 (1,1); Wuscanf"%d",&m); -F (I,1, M) About         { $scanf"%d%d",&x,&y); -x++,y++; -             intLca=LCA (x, y); -q[i]=Node (X,y,lca,dep[lca]); A         } +Sort (q+1, q+1+m); the         intans=0; -F (I,1, M) $         { the             intFlag= (Ask (dfsl[q[i].x) | |ask (Dfsl[q[i].y])); the             if(!flag) the             { theans++; -Add (Dfsl[q[i].lca],1); inAdd (dfsr[q[i].lca]+1,-1); the             } the         } Aboutprintf"%d\n", ans); the     } the     return 0; the}
View Code

Hdu 6203 Ping Ping Ping (greedy + tree-like array +dfs order)

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.