Description
There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would grow in the tree. Kaka likes Apple very much, so he has been carefully nurturing the Big Apple tree.
The tree has N forks which is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?
The topic is to give a tree, and then dynamically maintain the value of the node subtree and.
Originally very simple a problem, as long as the DFS record into the time and out of time, the results of my silly to use the tree chain to do, but also simple, is a naked question.
But forgot to initialize the tree-like array wa,%>_<% ...
The code is as follows:
//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 17 Friday 15:35 58 seconds//File name:3321.cpp#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn=100005;structedge{intTo,next;}; Edge E[MAXN<<1];intHead[maxn],ecou;intFA[MAXN],SON[MAXN],SIZ[MAXN],DEP[MAXN],TOP[MAXN],W[MAXN];intTcou;intC[MAXN];voidinit () {Tcou=1; Ecou=0; w[1]=1; top[1]=1; memset (Head,-1,sizeof(head)); memset (C,0,sizeof(C));}voidAddedge (intUintv) {e[ecou].to=v; E[ecou].next=Head[u]; Head[u]=ecou++;}voidDFS1 (intUintPreintd) { intv; Dep[u]=D; Fa[u]=Pre; Siz[u]=1; Son[u]=-1; for(inti=head[u];i!=-1; i=e[i].next)if(e[i].to!=pre) {v=e[i].to; DFS1 (V,u,d+1); Siz[u]+=Siz[v]; if(son[u]==-1|| siz[son[u]]<Siz[v]) Son[u]=W; }}voidDFS2 (intu) { if(son[u]==-1) return; Top[son[u]]=Top[u]; W[son[u]]=++Tcou; DFS2 (Son[u]); intv; for(inti=head[u];i!=-1; i=e[i].next)if(E[i].to!=son[u] && e[i].to!=Fa[u]) {v=e[i].to; TOP[V]=v; W[V]=++Tcou; DFS2 (v); }}intN;inlineintLowbit (intx) { returnx& (-x);}voidAddintXintd) { while(x<=N) {c[x]+=D; X+=lowbit (x); }}intSumintx) { intret=0; while(x>0) {ret+=C[x]; X-=lowbit (x); } returnret;}voidUpdateintUintVintd) { intF1=top[u],f2=Top[v]; while(f1!=F2) { if(dep[f1]<Dep[f2]) {swap (F1,F2); Swap (U,V); } add (W[f1],d); Add (W[u]+1,-d); U=FA[F1]; F1=Top[u]; } if(dep[u]>Dep[v]) swap (U,V); Add (w[u],d); Add (W[v]+1,-d);}BOOLREM[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intM; intb; Chars[Ten]; while(~SCANF ("%d",&N)) {init (); Memset (REM,0,sizeof(REM)); for(intI=1; i<n;++i) {scanf ("%d%d",&a,&b); Addedge (A, b); Addedge (B,a); } DFS1 (1,-1,1); DFS2 (1); scanf ("%d",&M); for(intI=1; i<=n;++i) Update (1I1); while(m--) {scanf ("%s%d",s,&a); if(s[0]=='Q') printf ("%d\n", SUM (W[a])); Else{Update (1, A,rem[a]?1:-1); Rem[a]=!Rem[a]; } } } return 0;}View Code
Simple POJ 3321 Apple trees, tree-chain + tree-shaped array.