#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define min(x,y) ((x)<(y)?(x):(y)) #define INF 0x3f3f3f3f int n,m,x,y,tot,dfn[120000],Min[120000],head[120000],f[1200000],bit[240000]; char ch; struct edge{int v,nxt;}e[240000]; void Insert(int x,int y){e[++tot].v=y; e[tot].nxt=head[x]; head[x]=tot;} void dfs(int x){ dfn[x]=-1; for (int i=head[x];i;i=e[i].nxt){ int v=e[i].v; if (!dfn[v]){ dfs(v); Min[x]=min(Min[x],Min[v]); } } dfn[x]=++tot; if (Min[x]==INF) Min[x]=tot; } void add(int x,int y){ for (int i=x;i<=n*2;i+=i&(-i)) bit[i]+=y; } int query(int x){ int sum=0; for (int i=x;i>=1;i-=i&(-i)) sum+=bit[i]; return sum; } int main(){ scanf("%d",&n); for (int i=1;i<n;i++){ scanf("%d%d",&x,&y); Insert(x,y); Insert(y,x); } memset(Min,INF,sizeof Min); tot=0; dfs(1); for (int i=1;i<=n;i++){f[i]=1; add(dfn[i],f[i]);} scanf("%d",&m); for (int i=1;i<=m;i++){ for (ch=getchar();ch!='Q' && ch!='C';ch=getchar()); scanf("%d",&x); if (ch=='Q') printf("%d\n",query(dfn[x])-query(Min[x]-1)); else{ f[x]=f[x]==1?-1:1; add(dfn[x],f[x]); } } return 0; }
|