[poj3417] Network (lca+ tree DP)

Source: Internet
Author: User

Test instructions: Give a tree without a root, and then give the M edge below, the m edge connected, each time you go to two sides, a rule is a tree edge, a new side, ask how many kinds of programs can make the tree break.

The key to solve the problem: the edge of the right to the point of power, record each side by the ring coverage of the number of times, through Val[a]++,val[b]++,val[lca (A, B)]-=2, to control each point above the edge, so the tree vertices to be removed.

A long time No 1 A, happy

1 //#pragma COMMENT (linker, "/stack:1024000000,1024000000")2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <iostream>8#include <vector>9typedefLong Longll;Ten using namespacestd;  One Const intmaxn=101000;  A Const intmaxm= -;  - int_pow[maxm],m,n;  - intHead[maxn],tot; the intver[maxn*2],depth[maxn*2],first[maxn],rmq[maxn*2][ -],id;//5 arrays, note which one needs to multiply by 2 - intPRE[MAXN],VAL[MAXN]; - intans; -InlineintRead () { +     Chark=0;CharLs;ls=getchar (); for(;ls<'0'|| Ls>'9'; k=ls,ls=GetChar ()); -     intx=0; for(; ls>='0'&&ls<='9'; Ls=getchar ()) x= (x<<3) + (x<<1) +ls-'0'; +     if(k=='-') x=0-X;returnx; A } at  - structedge{ -     intto,nxt;  -}e[maxn*2];//chain-forward star achievement -  - voidinit () { inmemset (head,-1,sizeofhead);  -tot=0; toId=0; +ans=0; - } the  * voidAdd_edge (intUintv) {   $e[tot].to=v;Panax Notoginsenge[tot].nxt=Head[u]; -head[u]=tot++; the } +    A voidDfsintUintFaintDEP) { theVer[++id]=u;//number of nodes visited by the first I +DEPTH[ID]=DEP;//the node depth to which I visited -first[u]=ID; $      for(inti=head[u];i!=-1; i=e[i].nxt) { $         intv=e[i].to; -         if(V==FA)Continue; -         //Pre[v]=u; theDFS (v,u,dep+1); -Ver[++id]=u;//Post -Traversal, revisit the parent nodeWuyidepth[id]=DEP; the     } - } Wu  - voidRmq_init (intN) { About     intk=int(log (n)/log (2)); $      for(intI=1; i<=n;++i) rmq[i][0]=i;  -      for(intj=1; j<=k;++j) {   -          for(intI=1; i+_pow[j]-1<=n;++i) {//because the index is stored -             inta=rmq[i][j-1],b=rmq[i+_pow[j-1]][j-1];  ARMQ[I][J]=DEPTH[A]&LT;DEPTH[B]?a:b; +         } the     } - } $    the intRmq_query (intLintR) { the     intk=int(Log (r-l+1.0)/log (2.0));  the     inta=rmq[l][k],b=rmq[r-_pow[k]+1][k];  the     returnDEPTH[A]&LT;DEPTH[B]?a:b; -}//The returned index is still in    the intLCA (intUintv) { the     intx=first[u],y=First[v];  About     if(x>y) Swap (x, y);  the     intres=rmq_query (x, y);  the     returnVer[res]; the } + voidDfsintUintFA) { -      for(inti=head[u];i!=-1; i=e[i].nxt) { the         intv=e[i].to;Bayi         if(V==FA)Continue; the DFS (v,u); theval[u]+=Val[v]; -     } -     if(val[u]==1) ans++; the     Else if(!val[u]&&u!=1) ans+=m; the } the  the intMain () { -      for(intI=0; i<maxm;++i) _pow[i]=1<<i;//pretreatment 2^n the     intk,a,b; theN=read (); m=read (); the init ();94     //for (int i=1;i<=n;i++) val[i]=read (); the      for(intI=0; i<n-1;++i) { theA=read (), b=read (); the Add_edge (A, b);98 Add_edge (b,a); About     } -Dfs1,-1,0);101Rmq_init (2*n-1);102      for(intI=0; i<m;++i) {103A=read (); b=read ();104Val[a]++,val[b]++,val[lca (A, b)]-=2; the     }106Dfs1,-1);107printf"%d\n", ans);108     109     return 0; the}

[poj3417] Network (lca+ tree DP)

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.