1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 110000;
struct Edge{
int to,nex;
}edge[MAXN*2];int ecnt = 2;
int fir[MAXN];
void addedge(int a,int b){
edge[ecnt] = (Edge){b,fir[a]};
fir[a] = ecnt++;
}
int n,q,id[MAXN],siz[MAXN],cnt;
int back[MAXN];
namespace SegTree{
#define lson (nown<<1)
#define rson (nown<<1|1)
#define mid ((l+r)>>1)
int maxn[MAXN<<2];
void build(int nown,int l,int r){
maxn[nown] = 1;
if(l == r) return;
build(lson,l,mid);
build(rson,mid+1,r);
}
void modify(int nown,int l,int r,int ql,int qr,int v){
if(ql <= l && r <= qr){
maxn[nown] = max(maxn[nown],v);
}
else{
if(ql <= mid)
modify(lson,l,mid,ql,qr,v);
if(qr >= mid+1)
modify(rson,mid+1,r,ql,qr,v);
}
}
int query(int nown,int l,int r,int pos){
if(l == r){
return maxn[nown];
}
else{
if(pos <= mid)
return max(maxn[nown],query(lson,l,mid,pos));
if(pos >= mid+1)
return max(maxn[nown],query(rson,mid+1,r,pos));
}
return 1;
}
}
void dfs(int nown,int fa){
id[nown] = ++cnt;
back[cnt] = nown;
siz[nown] = 1;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(v == fa) continue;
dfs(v,nown);
siz[nown] += siz[v];
}
}
void init(){
scanf("%d %d",&n,&q);
for(int i = 2;i<=n;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b),addedge(b,a);
}
}
void solve(){
dfs(1,0);
SegTree::build(1,1,n);
for(int i = 1;i<=q;i++){
char s[5];int x;
scanf("%s %d",s,&x);
if(s[0] == 'C'){
SegTree::modify(1,1,n,id[x],id[x]+siz[x]-1,id[x]);
}
if(s[0] == 'Q'){
int t = SegTree::query(1,1,n,id[x]);
printf("%d\n",back[t]);
}
}
}
int main(){
init();
solve();
return 0;
}
|