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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
| #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1009999999;
const int N = 200005;
int n, fa[N];
LL ans;
char str[5];
namespace DFS {
struct edge_type { int to, next; } edge[N << 1];
int cnte, h[N], dep[N], tid[N], dfn[N], clo;
void Init(int n) {
for (int i = 1; i <= n; ++i) h[i] = 0;
cnte = 1;
clo = 0;
}
void ins(int x, int y) {
edge[++cnte].to = y;
edge[cnte].next = h[x];
h[x] = cnte;
edge[++cnte].to = x;
edge[cnte].next = h[y];
h[y] = cnte;
}
void dfs(int now, int father, int deep) {
fa[now] = father;
tid[now] = ++clo;
dep[clo] = deep;
for (int i = h[now]; i; i = edge[i].next)
if (edge[i].to != father)
dfs(edge[i].to, now, deep+1);
dfn[now] = clo;
}
}
namespace SGT {
LL sum[N << 2], tag[N << 2];
void build(int now, int l, int r) {
tag[now] = 0;
if (l == r) {
sum[now] = DFS::dep[l];
return;
}
int mid = (l + r) >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
sum[now] = sum[now << 1] + sum[now << 1 | 1];
}
void add(int now, int l, int r, int val) {
if (l != r) tag[now] += val;
sum[now] += val * (r - l + 1);
}
void pd(int now, int l, int mid, int r) {
add(now << 1, l, mid, tag[now]);
add(now << 1 | 1, mid + 1, r, tag[now]);
tag[now] = 0;
}
void query(int now, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
ans += sum[now];
return;
}
int mid = (l + r) >> 1;
if (tag[now]) pd(now, l, mid, r);
if (ll <= mid) query(now << 1, l, mid, ll, rr);
if (rr > mid) query(now << 1 | 1, mid + 1, r, ll, rr);
}
void change(int now, int l, int r, int ll, int rr, int val) {
if (ll <= l && r <= rr) {
add(now, l, r, val);
return;
}
int mid = (l + r) >> 1;
if (tag[now]) pd(now, l, mid, r);
if (ll <= mid) change(now << 1, l, mid, ll, rr, val);
if (rr > mid) change(now << 1 | 1, mid + 1, r, ll, rr, val);
sum[now] = sum[now << 1] + sum[now << 1 | 1];
}
}
namespace LCT {
int c[N][2];
void Init(int n) {
for (int i = 1; i <= n; ++i)
c[i][0] = c[i][1] = fa[i] = 0;
}
bool isroot(int x) {
return c[fa[x]][0] != x && c[fa[x]][1] != x;
}
void rotate(int x) {
int y = fa[x], z = fa[y], a = c[y][1] == x, b = a ^ 1;
if (!isroot(y)) c[z][c[z][1]==y] = x;
fa[x] = z; fa[y] = x;
if (c[x][b]) fa[c[x][b]] = y;
c[y][a] = c[x][b]; c[x][b] = y;
}
void splay(int x) {
int y;
while (!isroot(x)) {
y = fa[x];
if (!isroot(y)) {
if (c[fa[y]][0] == y ^ c[y][0] == x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
int Mag(int x, int j) {
while (c[x][j]) x = c[x][j];
return x;
}
void access(int x) {
int y = 0, z;
while (x) {
splay(x);
if (c[x][1]) {
z = Mag(c[x][1], 0);
SGT::change(1, 1, n, DFS::tid[z], DFS::dfn[z], 1);
}
if (y) {
z = Mag(y, 0);
SGT::change(1, 1, n, DFS::tid[z], DFS::dfn[z], -1);
}
c[x][1] = y;
y = x;
x = fa[x];
}
}
}
namespace HSK {
int x, y, m, T;
int getint() {
int r = 0, k = 1; char c = getchar();
for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
return r * k;
}
void work() {
T = getint();
while (T--) {
n = getint();
LCT::Init(n);
DFS::Init(n);
for (int i = 1; i < n; ++i) {
x = getint() + 1;
y = getint() + 1;
DFS::ins(x, y);
}
DFS::dfs(1, 0, 0);
SGT::build(1, 1, n);
m = getint();
while (m--) {
scanf("%s", str);
x = getint() + 1;
if (str[0] == 'O') LCT::access(x);
else {
ans = 0;
SGT::query(1, 1, n, DFS::tid[x], DFS::dfn[x]);
printf("%lld\n", ans);
}
}
}
}
}
int main() {
freopen("kongzhi.in", "r", stdin);
freopen("kongzhi.out", "w", stdout);
HSK::work();
fclose(stdin);
fclose(stdout);
return 0;
}
|