#include <stdio.h>
#include <string.h>
struct node
{
int to ;
int next ;
};
bool is_used[100001] ;
node edge[100001 * 2] ;
int head[100001] ;
char useless[5] ;
int s[100001] ;
int e[100001] ;
int c[100001] ;
int index ;
int cnt ;
int n ;
void init()
{
memset(head , -1 , sizeof(head)) ;
cnt = 1 ;
}
void edgeadd(int from , int to)
{
edge[cnt].to = to ;
edge[cnt].next = head[from] ;
head[from] = cnt++ ;
}
void dfs(int fa , int too)
{
s[too] = ++index ;
for(int i = head[too] ; i != -1 ; i = edge[i].next)
{
int to = edge[i].to ;
if(to != fa)
{
dfs(too , to) ;
}
}
e[too] = index ;
}
int lowbit(int x)
{
return x & (-x) ;
}
void updata(int x , int tmp)
{
while(x <= n)
{
c[x] += tmp ;
x += lowbit(x) ;
}
}
int getsum(int x)
{
int sum = 0 ;
while(x)
{
sum += c[x] ;
x -= lowbit(x) ;
}
return sum ;
}
int main()
{
scanf("%d" , &n) ;
init() ;
for(int i = 1 ; i < n ; i++)
{
int x , y ;
scanf("%d%d" , &x , &y) ;
edgeadd(x , y) ;
edgeadd(y , x) ;
}
dfs(-1 , 1) ;
for(int i = 1 ; i <= n ; i++)
{
updata(i , 1) ;
is_used[i] = 1 ;
}
int q ;
scanf("%d" , &q) ;
for(int i = 1 ; i <= q ; i++)
{
scanf("%s" , useless) ;
if(useless[0] == ‘C‘)
{
int x ;
scanf("%d" , &x) ;
if(is_used[x] == 1)
{
is_used[x] = 0 ;
updata(s[x] , -1) ;
}else
{
is_used[x] = 1 ;
updata(s[x] , 1) ;
}
}else
{
int x ;
scanf("%d" , &x) ;
printf("%d\n" , getsum(e[x]) - getsum(s[x] - 1)) ;
}
}
}
原文地址:http://blog.csdn.net/wzq_qwq/article/details/45420691