I found that the last time is a tree array...
Http://poj.org/problem? Id = 3321
Given a tree, its root node is always 1, and each node value is 1 at the beginning. There are two operations: one is to query the total number of nodes under the node, the second is to reverse the node value (1 to 0, 0 to 1 )...
This is similar to hdu3887 a few days ago... This method is used to calculate the number of reverse orders .. However, this question uses a method to re-create a sequence for the node (someone called a timestamp). It is a method to traverse the node in the descending order to create another number, in this way, we record the number B when we first enter this node and the number C when we leave this node. Then we can ensure that the node number on a node subtree must be smaller than its number, and the value we maintain is the sum of the range [B, c...
Note that this question is stuck with STL .... At last, I wrote my own neighbor table for 390ms... I also simulate recursion on my own stack. It seems that direct DFS will not blow up the stack...
Code:
#include<vector>#include<iostream>using namespace std;const int N=200010;int n, q, sum[N], vis[N], b[N], c[N], cn, stk[N], a[N];int first[N], en;struct node{int v, next;} edge[N];int query(int i){int tmp=0;for(; i>0; i-=i&(-i))tmp += sum[i];return tmp;}void update(int i, int v){for(; i<=n; i+=i&(-i))sum[i] += v;}void dfs(){int i, j, sn;for(i=0; i<=n; i++)vis[i] = 0;sn = 0;cn = 1;stk[sn++] = 1;vis[1] = 1;while(sn!=0){i = stk[sn-1];if(vis[i]==1){b[i] = cn;vis[i] = 2;}if(first[i]!=-1){j = first[i];if(vis[edge[j].v]==0){stk[sn++] = edge[j].v;vis[edge[j].v] = 1;}first[i] = edge[j].next;}else{c[i] = cn++;sn--;}}}void add(int x, int y){edge[en].v = y;edge[en].next = first[x];first[x] = en++;}int main(){int i, x, y, v;char op[3];while(scanf("%d", &n)!=EOF){if(n==0)return 0;for(i=0; i<=2*n; i++)first[i] = -1;en = 0;for(i=1; i<n; i++){scanf("%d%d", &x, &y);add(x, y);add(y, x);}dfs();for(i=0; i<=n; i++)sum[i] = 0;for(i=1; i<=n; i++){update(i, 1);a[i] = 1;}scanf("%d", &q);while(q--){scanf("%s %d", op, &v);if(op[0]=='Q')printf("%d\n", query(c[v])-query(b[v]-1));else{if(a[c[v]]==1){update(c[v], -1);a[c[v]] = 0;}else{update(c[v], 1);a[c[v]] = 1;}}}}return 0;}