2013
11-12

# Apple Tree

There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will 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 are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won’t grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N – 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

For every inquiry, output the correspond answer per line.

3
1 2
1 3
3
Q 1
C 2
Q 1


3
2


//* @author:

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

class cin
{
static StringTokenizer st;
static int leave=0;
static int nextInt() throws IOException
{
while(leave==0)
{
leave=st.countTokens();
}
leave--;
return Integer.parseInt(st.nextToken());
}
static String nextString() throws IOException
{
while(leave==0)
{
leave=st.countTokens();
}
leave--;
return st.nextToken();
}
static boolean hasNext() throws IOException
{
while(leave==0)
{
if(temp==null)return false;
st=new StringTokenizer(temp);
leave=st.countTokens();
}
return true;
}
}

class Node //树邻接表
{
int now,v=1;
Node next;
Node(int y)
{
now=y;
next=null;
}
}

class TreeArray //数状数组
{
int value[],n;
int now;
Node tree[];
int pis[][];
TreeArray(int num,Node e[])
{
n=num;
value=new int[n+1];
tree=e;
pis=new int[n+1][2];
Arrays.fill(value,0);
}

int lowBit(int t)
{
return t&(t^(t-1));
}

void plus(int a,int i)
{
while(i<=n)
{
value[i]+=a;
i+=lowBit(i);
}
}

int getSum(int i)
{
int sum=0;
while(i>0)
{
sum+=value[i];
i=i-lowBit(i);
}
return sum;
}
void dfs(int t) //dfs记录时间
{
pis[t][0]=now;
now++;
Node p=tree[t].next;
while(p!=null)
{
dfs(p.now);
p=p.next;
}
pis[t][1]=now;

}
void init()
{
now=0;
dfs(1);
for(int i=1;i<=n;i++) //初始化树状树组
{
plus(1,i);
}
}
}

public class Main {
public static void main(String args[]) throws IOException
{
int n,t1,t2,i;
String s;
n=cin.nextInt();
Node lunch[]=new Node[n+1],temp;
for(i=1;i<=n;i++)
{
lunch[i]=new Node(i);
}
for(i=0;i< n-1;i++)
{
t1=cin.nextInt();
t2=cin.nextInt();
temp=new Node(t2);
temp.next=lunch[t1].next;
lunch[t1].next=temp;
}
TreeArray data=new TreeArray(n,lunch);
data.init();
n=cin.nextInt();
for(i=0;i< n;i++)
{
s=cin.nextString();
t1=cin.nextInt();
if(s.charAt(0)=='C')
{
if(data.tree[t1].v==1)
{
data.tree[t1].v=0;
data.plus(-1,data.pis[t1][0]+1);
}
else
{
data.tree[t1].v=1;
data.plus(1,data.pis[t1][0]+1);
}
}
else
{
t2=data.getSum(data.pis[t1][0]);
int t3=data.getSum(data.pis[t1][1]);
System.out.println(t3-t2);
}
}
}
}

1. 老实说，这种方法就是穷举，复杂度是2^n，之所以能够AC是应为题目的测试数据有问题，要么数据量很小，要么能够得到k == t，否则即使n = 30，也要很久才能得出结果，本人亲测