#include <bits/stdc++.h>
using namespace std;
const int Z = 1.2e5, B = 17;
int N, K, p[Z][B], up[Z][2], d[Z], pE[Z], res[Z];
vector<int> h[Z];
vector<array<int, 2>> g[Z], q0[Z], q1[Z];
vector<array<int, 3>> qQ;
vector<array<int, 4>> pQ;
void init(int u) {
for(int i = 0; i + 1 < B; ++i)
p[u][i+1] = p[p[u][i]][i];
for(auto [v, i] : g[u]) if(v != p[u][0]) {
p[v][0] = u;
d[v] = d[u] + 1;
pE[v] = i;
if(pE[u] < i) {
up[v][0] = up[u][0];
up[v][1] = u;
} else {
up[v][0] = u;
up[v][1] = up[u][1];
}
init(v);
}
}
int _u, _v;
int lca(int u, int v) {
bool swapped = 0;
if((swapped = (d[u] < d[v]))) swap(u, v);
if(d[u] > d[v]) {
for(int i = B; i--; ) if((d[u] - d[v] - 1) & (1<<i))
u = p[u][i];
if(p[u][0] == v) {
_u = u;
return v;
}
u = p[u][0];
}
for(int i = B; i--; ) if(p[u][i] != p[v][i])
u = p[u][i], v = p[v][i];
if(swapped) swap(u, v);
_u = u, _v = v;
return p[u][0];
}
int F[2*Z];
void add(int i, int v) {
for(++i; i < N + K; i += i&-i) F[i] += v;
}
int get(int i) {
int v {};
for(++i; i; i -= i&-i) v += F[i];
return v;
}
void dfs(int u) {
add(pE[u], 1);
for(auto [j, i] : q1[u])
res[j] -= get(i);
for(auto [v, i] : g[u]) if(v != p[u][0])
dfs(v);
for(auto [j, i] : q0[u])
res[j] += get(i);
for(int v : h[u])
add(pE[v], -1);
}
int main() {
cin >> N >> K;
for(int i {}, j {}; i < N - 1 + K; ++i) {
char t; int u, v;
cin >> t >> u; --u;
if(t == 'S') {
cin >> v; --v;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
if(t == 'Q') {
cin >> v; --v;
pQ.push_back({j++, i, u, v});
}
if(t == 'C')
qQ.push_back({j++, i, u});
}
for(int u = N; u--; )
reverse(begin(g[u]), end(g[u]));
init(0);
for(auto [j, i, u] : qQ) {
q0[u].push_back({j, i});
q1[up[u][1]].push_back({j, i});
}
for(int u = N; u--; )
h[up[u][0]].push_back(u);
dfs(0);
for(auto [j, i, u, v] : pQ) {
int w = lca(u, v), val = w == u ? pE[_u] : pE[u];
if(w != u && w != v && pE[_u] < pE[_v])
val = i;
if(u == v || (max(d[up[u][0]], d[up[v][1]]) <= d[w] && val < i))
res[j] = -1;
else
res[j] = -2;
}
for(int j = 0; j < K; ++j) {
if(res[j] < 0) cout << (res[j] & 1 ? "yes" : "no");
else cout << 1 + res[j];
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
14876 KB |
Output is correct |
2 |
Correct |
95 ms |
15364 KB |
Output is correct |
3 |
Correct |
108 ms |
15488 KB |
Output is correct |
4 |
Correct |
113 ms |
15540 KB |
Output is correct |
5 |
Correct |
90 ms |
15316 KB |
Output is correct |
6 |
Correct |
98 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
14876 KB |
Output is correct |
2 |
Correct |
95 ms |
15364 KB |
Output is correct |
3 |
Correct |
108 ms |
15488 KB |
Output is correct |
4 |
Correct |
113 ms |
15540 KB |
Output is correct |
5 |
Correct |
90 ms |
15316 KB |
Output is correct |
6 |
Correct |
98 ms |
15184 KB |
Output is correct |
7 |
Correct |
76 ms |
15000 KB |
Output is correct |
8 |
Correct |
85 ms |
16596 KB |
Output is correct |
9 |
Correct |
76 ms |
16644 KB |
Output is correct |
10 |
Correct |
94 ms |
16784 KB |
Output is correct |
11 |
Correct |
97 ms |
16496 KB |
Output is correct |
12 |
Correct |
91 ms |
16244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15000 KB |
Output is correct |
2 |
Correct |
239 ms |
30832 KB |
Output is correct |
3 |
Correct |
247 ms |
30912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15000 KB |
Output is correct |
2 |
Correct |
239 ms |
30832 KB |
Output is correct |
3 |
Correct |
247 ms |
30912 KB |
Output is correct |
4 |
Correct |
72 ms |
15004 KB |
Output is correct |
5 |
Correct |
256 ms |
31336 KB |
Output is correct |
6 |
Correct |
157 ms |
32992 KB |
Output is correct |
7 |
Correct |
189 ms |
32936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
14880 KB |
Output is correct |
2 |
Correct |
254 ms |
40252 KB |
Output is correct |
3 |
Correct |
276 ms |
40248 KB |
Output is correct |
4 |
Correct |
237 ms |
39380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
14880 KB |
Output is correct |
2 |
Correct |
254 ms |
40252 KB |
Output is correct |
3 |
Correct |
276 ms |
40248 KB |
Output is correct |
4 |
Correct |
237 ms |
39380 KB |
Output is correct |
5 |
Correct |
69 ms |
15024 KB |
Output is correct |
6 |
Correct |
279 ms |
42724 KB |
Output is correct |
7 |
Correct |
224 ms |
43708 KB |
Output is correct |
8 |
Correct |
294 ms |
43944 KB |
Output is correct |
9 |
Correct |
271 ms |
43936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
14936 KB |
Output is correct |
2 |
Correct |
237 ms |
30852 KB |
Output is correct |
3 |
Correct |
244 ms |
31432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
14936 KB |
Output is correct |
2 |
Correct |
237 ms |
30852 KB |
Output is correct |
3 |
Correct |
244 ms |
31432 KB |
Output is correct |
4 |
Correct |
79 ms |
15004 KB |
Output is correct |
5 |
Correct |
276 ms |
32996 KB |
Output is correct |
6 |
Correct |
263 ms |
33668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
14860 KB |
Output is correct |
2 |
Correct |
262 ms |
40292 KB |
Output is correct |
3 |
Correct |
270 ms |
40244 KB |
Output is correct |
4 |
Correct |
225 ms |
39388 KB |
Output is correct |
5 |
Correct |
82 ms |
14932 KB |
Output is correct |
6 |
Correct |
237 ms |
30960 KB |
Output is correct |
7 |
Correct |
248 ms |
31512 KB |
Output is correct |
8 |
Correct |
326 ms |
31296 KB |
Output is correct |
9 |
Correct |
268 ms |
31788 KB |
Output is correct |
10 |
Correct |
337 ms |
35468 KB |
Output is correct |
11 |
Correct |
314 ms |
34328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
14860 KB |
Output is correct |
2 |
Correct |
262 ms |
40292 KB |
Output is correct |
3 |
Correct |
270 ms |
40244 KB |
Output is correct |
4 |
Correct |
225 ms |
39388 KB |
Output is correct |
5 |
Correct |
82 ms |
14932 KB |
Output is correct |
6 |
Correct |
237 ms |
30960 KB |
Output is correct |
7 |
Correct |
248 ms |
31512 KB |
Output is correct |
8 |
Correct |
326 ms |
31296 KB |
Output is correct |
9 |
Correct |
268 ms |
31788 KB |
Output is correct |
10 |
Correct |
337 ms |
35468 KB |
Output is correct |
11 |
Correct |
314 ms |
34328 KB |
Output is correct |
12 |
Correct |
65 ms |
14936 KB |
Output is correct |
13 |
Correct |
270 ms |
42828 KB |
Output is correct |
14 |
Correct |
249 ms |
43640 KB |
Output is correct |
15 |
Correct |
264 ms |
43944 KB |
Output is correct |
16 |
Correct |
293 ms |
44044 KB |
Output is correct |
17 |
Correct |
74 ms |
14924 KB |
Output is correct |
18 |
Correct |
227 ms |
33068 KB |
Output is correct |
19 |
Correct |
284 ms |
33688 KB |
Output is correct |
20 |
Correct |
266 ms |
33520 KB |
Output is correct |
21 |
Correct |
314 ms |
33992 KB |
Output is correct |
22 |
Correct |
296 ms |
37396 KB |
Output is correct |
23 |
Correct |
323 ms |
37612 KB |
Output is correct |
24 |
Correct |
293 ms |
36056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
14872 KB |
Output is correct |
2 |
Correct |
108 ms |
15424 KB |
Output is correct |
3 |
Correct |
104 ms |
15504 KB |
Output is correct |
4 |
Correct |
101 ms |
15452 KB |
Output is correct |
5 |
Correct |
97 ms |
15316 KB |
Output is correct |
6 |
Correct |
104 ms |
15168 KB |
Output is correct |
7 |
Correct |
74 ms |
14932 KB |
Output is correct |
8 |
Correct |
214 ms |
30836 KB |
Output is correct |
9 |
Correct |
295 ms |
30888 KB |
Output is correct |
10 |
Correct |
66 ms |
14920 KB |
Output is correct |
11 |
Correct |
261 ms |
40268 KB |
Output is correct |
12 |
Correct |
264 ms |
40380 KB |
Output is correct |
13 |
Correct |
221 ms |
39416 KB |
Output is correct |
14 |
Correct |
73 ms |
14828 KB |
Output is correct |
15 |
Correct |
252 ms |
30900 KB |
Output is correct |
16 |
Correct |
258 ms |
31528 KB |
Output is correct |
17 |
Correct |
323 ms |
31288 KB |
Output is correct |
18 |
Correct |
289 ms |
31868 KB |
Output is correct |
19 |
Correct |
346 ms |
35484 KB |
Output is correct |
20 |
Correct |
337 ms |
34296 KB |
Output is correct |
21 |
Correct |
243 ms |
30768 KB |
Output is correct |
22 |
Correct |
240 ms |
30704 KB |
Output is correct |
23 |
Correct |
265 ms |
31064 KB |
Output is correct |
24 |
Correct |
261 ms |
31056 KB |
Output is correct |
25 |
Correct |
295 ms |
33356 KB |
Output is correct |
26 |
Correct |
267 ms |
31876 KB |
Output is correct |
27 |
Correct |
278 ms |
31672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
14872 KB |
Output is correct |
2 |
Correct |
108 ms |
15424 KB |
Output is correct |
3 |
Correct |
104 ms |
15504 KB |
Output is correct |
4 |
Correct |
101 ms |
15452 KB |
Output is correct |
5 |
Correct |
97 ms |
15316 KB |
Output is correct |
6 |
Correct |
104 ms |
15168 KB |
Output is correct |
7 |
Correct |
74 ms |
14932 KB |
Output is correct |
8 |
Correct |
214 ms |
30836 KB |
Output is correct |
9 |
Correct |
295 ms |
30888 KB |
Output is correct |
10 |
Correct |
66 ms |
14920 KB |
Output is correct |
11 |
Correct |
261 ms |
40268 KB |
Output is correct |
12 |
Correct |
264 ms |
40380 KB |
Output is correct |
13 |
Correct |
221 ms |
39416 KB |
Output is correct |
14 |
Correct |
73 ms |
14828 KB |
Output is correct |
15 |
Correct |
252 ms |
30900 KB |
Output is correct |
16 |
Correct |
258 ms |
31528 KB |
Output is correct |
17 |
Correct |
323 ms |
31288 KB |
Output is correct |
18 |
Correct |
289 ms |
31868 KB |
Output is correct |
19 |
Correct |
346 ms |
35484 KB |
Output is correct |
20 |
Correct |
337 ms |
34296 KB |
Output is correct |
21 |
Correct |
243 ms |
30768 KB |
Output is correct |
22 |
Correct |
240 ms |
30704 KB |
Output is correct |
23 |
Correct |
265 ms |
31064 KB |
Output is correct |
24 |
Correct |
261 ms |
31056 KB |
Output is correct |
25 |
Correct |
295 ms |
33356 KB |
Output is correct |
26 |
Correct |
267 ms |
31876 KB |
Output is correct |
27 |
Correct |
278 ms |
31672 KB |
Output is correct |
28 |
Correct |
75 ms |
14960 KB |
Output is correct |
29 |
Correct |
82 ms |
16580 KB |
Output is correct |
30 |
Correct |
78 ms |
16636 KB |
Output is correct |
31 |
Correct |
88 ms |
16688 KB |
Output is correct |
32 |
Correct |
97 ms |
16552 KB |
Output is correct |
33 |
Correct |
87 ms |
16196 KB |
Output is correct |
34 |
Correct |
70 ms |
14976 KB |
Output is correct |
35 |
Correct |
232 ms |
31356 KB |
Output is correct |
36 |
Correct |
176 ms |
33056 KB |
Output is correct |
37 |
Correct |
176 ms |
33040 KB |
Output is correct |
38 |
Correct |
67 ms |
14936 KB |
Output is correct |
39 |
Correct |
281 ms |
42676 KB |
Output is correct |
40 |
Correct |
226 ms |
43696 KB |
Output is correct |
41 |
Correct |
286 ms |
43964 KB |
Output is correct |
42 |
Correct |
269 ms |
44028 KB |
Output is correct |
43 |
Correct |
82 ms |
14904 KB |
Output is correct |
44 |
Correct |
226 ms |
33092 KB |
Output is correct |
45 |
Correct |
267 ms |
33660 KB |
Output is correct |
46 |
Correct |
277 ms |
33560 KB |
Output is correct |
47 |
Correct |
294 ms |
33924 KB |
Output is correct |
48 |
Correct |
301 ms |
37388 KB |
Output is correct |
49 |
Correct |
339 ms |
37572 KB |
Output is correct |
50 |
Correct |
289 ms |
36036 KB |
Output is correct |
51 |
Correct |
203 ms |
33084 KB |
Output is correct |
52 |
Correct |
213 ms |
33400 KB |
Output is correct |
53 |
Correct |
182 ms |
32836 KB |
Output is correct |
54 |
Correct |
187 ms |
32992 KB |
Output is correct |
55 |
Correct |
209 ms |
33004 KB |
Output is correct |
56 |
Correct |
227 ms |
32144 KB |
Output is correct |
57 |
Correct |
285 ms |
34548 KB |
Output is correct |
58 |
Correct |
277 ms |
34488 KB |
Output is correct |
59 |
Correct |
266 ms |
32076 KB |
Output is correct |