# BZOJ1103: [POI2007] Metropolitan Meg

Source: Internet
Author: User

The DFS sequence.

Use L[u] and r[u] to indicate the time to enter u and go out U. Use a tree-like array to maintain prefixes and l[u] minus 1 at 1,r[u].

The question is the distance from the root to yourself, which is equivalent to the prefix of L[u]. Why is it?

If a node V is on the root-to-u path, it will add 1 to the l[v] place. If not, if the numbers are less than u,l[v] and r[v] are eliminated.

L[V] greater than l[u] if it is greater than U. W u operation is to L[u] minus 1, will r[u] place plus 1. This will not affect the path length.

The ingenious application of the DFS sequence.

`#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =250000+Ten;Const intMAXM =500000+Ten;structBIT {inta[maxn<<1],n; InlineintLowbit (intx) {return(x& (-x)); }        voidAddintXintv) { for(; x<=n;x+=lowbit (x)) a[x]+=v; }        intSumintx) {intres=0;  for(; x;x-=lowbit (x)) res+=A[x]; returnRes; }        voidInitintm) {memset (A,0,sizeof(a)); N=m; }} bit;intG[maxn],v[maxm],next[maxm],eid;intFA[MAXN],L[MAXN],R[MAXN],S[MAXN],SP,DFN;intn,m;voidAddedge (intAintb) {V[eid]=b; Next[eid]=g[a]; g[a]=eid++; V[eid]=a; NEXT[EID]=G[B]; g[b]=eid++;}voidDfs () {s[++sp]=1;  while(sp) {intu=s[sp--]; if(!L[u]) {L[u]=++DFN; s[++sp]=T;  for(intI=g[u];~i;i=next[i])if(v[i]!=Fa[u]) {Fa[v[i]]=u; s[++sp]=V[i]; }        }        Elser[u]=++DFN; }}intMain () {scanf ("%d",&N); Memset (g,-1,sizeof(g)); Bit.init (n*2);  for(intI=1, a,b;i<n;i++) {scanf ("%d%d",&a,&b);    Addedge (A, b);        } dfs ();  for(intI=2; i<=n;i++) {Bit.add (l[i],1); Bit.add (R[i],-1); }    Charop[Ten]; scanf ("%d", &m); m+= (n1);  for(intI=1, a,b;i<=m;i++) {scanf ("%s%d",s,&a); if(s[0]=='A') {scanf ("%d",&C); if(a>b) Swap (A, a); Bit.add (L[b],-1); Bit.add (R[b],1); }        Elseprintf"%d\n", Bit.sum (L[a])); }    return 0; }`

BZOJ1103: [POI2007] Metropolitan Meg

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.