Title Link: http://www.lydsy.com:808/JudgeOnline/problem.php?id=1103
The main idea: to give you a tree, just start all the tree edge Benquan are 1, and constantly change some of the edge of the right to 0, in the meantime asked you a point to the root node between the path of Benquan and.
This question is similar to the apple Tree of POJ ...
First DFS generates the topological order of the entire tree, DFS when each node I enters the time l[i] and the time of departure r[i], and then for each change operation, the tree array can be maintained.
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #define Maxe 501000#define maxv 251000#define lowbit (x) (x& (x)) using namespace std;struct edge{int u , V,next;} Edges[maxe];int Head[maxv],ncount=0;int SUM[MAXE],CNT=0,L[MAXV],R[MAXV],FA[MAXV]; L, R is the left and right interval int n,m;bool visit[maxv];void addedge (int u,int V) of each node Dfs sequence {edges[++ncount].u=u; Edges[ncount].v=v; Edges[ncount].next=head[u]; Head[u]=ncount;} void update (int x,int v) {while (x<maxe) {sum[x]+=v; X+=lowbit (x); }}int query (int x) {int ans=0; while (x>0) {ans+=sum[x]; X-=lowbit (x); } return ans; void Dfs (int u)//Start Dfs from point u, get the topological order of the tree {visit[u]=true; l[u]=++cnt; Update (cnt,1); for (int p=head[u];p!=-1;p=edges[p].next) {int v=edges[p].v; if (!visit[v]) {fa[v]=u; DFS (v); }} r[u]=++cnt; Update (CNT,-1);} int main () {MemseT (head,-1,sizeof (head)); Char cmd[10]; scanf ("%d", &n); for (int i=1;i<n;i++) {int A, B; scanf ("%d%d", &a,&b); Addedge (A, b); Addedge (B,a); } dfs (1); scanf ("%d", &m); for (int i=1;i<=n+m-1;i++) {int A, B; scanf ("%s%d", cmd,&a); if (cmd[0]== ' A ')//A-B edge contingency is 0 {scanf ("%d", &b); if (fa[a]==b) swap (A, b); Ensure that the father of B is a update (L[B],-1); Update (r[b],1); } else printf ("%d\n", Query (L[a])-1); } return 0;}
[Bzoj 1103] [POI 2007] Metropolis (Dfs seeking topological order + Tree Array)