1 #include <bits/stdc++.h>
2 using namespace std;
3 inline int read(){
4 int x=0;int f=1;char ch=getchar();
5 while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();}
6 while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}
7 return x*f;
8 }
9 const int MAXN=1e6+10;
10 struct sigment_tree{
11 int sum;
12 }T[MAXN];
13 namespace zhangenming{
14 struct node{
15 int y,next;
16 }e[MAXN<<1];
17
18 int linkk[MAXN<<1],c[MAXN<<1]={},siz[MAXN]={},fa[MAXN],dep[MAXN]={},son[MAXN],Node[MAXN],cnt[MAXN]={},top[MAXN]={},tot=0,m,len=0,n,x,y;
19 void insert(int xx,int yy){
20 e[++len].y=yy;e[len].next=linkk[xx];linkk[xx]=len;
21 }
22 void dfs1(int father,int node,int depth){
23 siz[node]=1;fa[node]=father;dep[node]=depth;son[node]=0;int maxxn=0;
24 for(int i=linkk[node];i;i=e[i].next){
25 if(e[i].y!=father){
26 dfs1(node,e[i].y,depth+1);
27 if(siz[e[i].y]>maxxn) son[node]=e[i].y,maxxn=siz[e[i].y];
28 siz[node]+=siz[e[i].y];
29 }
30 }
31 //cout<<siz[node]<<endl;
32 }
33 void dfs2(int node,int id){
34 cnt[node]=++tot;Node[tot]=node;top[node]=id;
35 if(son[node]) dfs2(son[node],id);
36 for(int i=linkk[node];i;i=e[i].next){
37 if(e[i].y!=fa[node]&&e[i].y!=son[node])
38 dfs2(e[i].y,e[i].y);
39 }
40 }
41 void buildtree(int l,int r,int root){
42 if(l==r) {T[root].sum=1;if(l==1) T[root].sum=0;return;}
43 int mid=(l+r)>>1;
44 buildtree(l,mid,root<<1);
45 buildtree(mid+1,r,root<<1|1);
46 T[root].sum=T[root<<1].sum+T[root<<1|1].sum;
47 }
48 void insertt(int l,int r,int root){
49 if(l>x||r<x) return;
50 if(l==r) {T[root].sum=0;return;}
51 int mid=(l+r)>>1;
52 insertt(l,mid,root<<1);
53 insertt(mid+1,r,root<<1|1);
54 T[root].sum=T[root<<1].sum+T[root<<1|1].sum;
55 }
56 int find(int l,int r,int root){
57 if(l>=x&&r<=y) return T[root].sum;
58 if(l>y||r<x) return 0;
59 int mid=(r+l)>>1;
60 return find(l,mid,root<<1)+find(mid+1,r,root<<1|1);
61 }
62 void init(){
63 n=read();
64 for(int i=1;i<n;i++){
65 int xx=read();int yy=read();
66 insert(xx,yy);
67 insert(yy,xx);
68 }
69 dfs1(0,1,0);
70 dfs2(1,1);
71 buildtree(1,n,1);
72 }
73 int search(int node){
74 if(node==1) return 0;
75 int ans=0;
76 while(top[node]!=1){
77 x=cnt[top[node]];
78 y=cnt[node];
79 ans+=find(1,n,1);
80 node=fa[top[node]];
81 }
82 if(node==1) return ans;
83 y=cnt[node];
84 x=cnt[top[node]];
85 ans+=find(1,n,1);
86 return ans;
87 }
88 void solve(){
89 m=read()+n-1;
90 while(m--){
91 char ch;
92 scanf("%c",&ch);
93 if(ch==‘A‘){
94 int xx=read();int yy=read();
95 if(fa[xx]==yy) swap(xx,yy);
96 x=cnt[yy];
97 insertt(1,n,1);
98 }
99 if(ch==‘W‘){
100 int xx=read();
101 printf("%d\n",search(xx));
102 }
103 }
104 }
105 }
106 int main(){
107 using namespace zhangenming;
108 init();
109 /*for(int i=1;i<=n;i++){
110 cout<<cnt[i]<<‘ ‘<<top[i]<<‘ ‘<<fa[i]<<‘ ‘<<son[i]<<‘ ‘<<Node[i]<<‘ ‘<<siz[i]<<‘ ‘<<endl;
111 }*/
112 solve();
113 return 0;
114 }