#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\n" : "no\n");
else cout << 1 + res[j] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
16344 KB |
Output is correct |
2 |
Correct |
65 ms |
17128 KB |
Output is correct |
3 |
Correct |
62 ms |
17160 KB |
Output is correct |
4 |
Correct |
70 ms |
17344 KB |
Output is correct |
5 |
Correct |
65 ms |
17360 KB |
Output is correct |
6 |
Correct |
69 ms |
17592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
16344 KB |
Output is correct |
2 |
Correct |
65 ms |
17128 KB |
Output is correct |
3 |
Correct |
62 ms |
17160 KB |
Output is correct |
4 |
Correct |
70 ms |
17344 KB |
Output is correct |
5 |
Correct |
65 ms |
17360 KB |
Output is correct |
6 |
Correct |
69 ms |
17592 KB |
Output is correct |
7 |
Correct |
49 ms |
16320 KB |
Output is correct |
8 |
Correct |
58 ms |
18100 KB |
Output is correct |
9 |
Correct |
53 ms |
18132 KB |
Output is correct |
10 |
Correct |
62 ms |
18124 KB |
Output is correct |
11 |
Correct |
70 ms |
18316 KB |
Output is correct |
12 |
Correct |
54 ms |
17932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
16320 KB |
Output is correct |
2 |
Correct |
155 ms |
33556 KB |
Output is correct |
3 |
Correct |
166 ms |
33748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
16320 KB |
Output is correct |
2 |
Correct |
155 ms |
33556 KB |
Output is correct |
3 |
Correct |
166 ms |
33748 KB |
Output is correct |
4 |
Correct |
46 ms |
16312 KB |
Output is correct |
5 |
Correct |
154 ms |
34048 KB |
Output is correct |
6 |
Correct |
112 ms |
34676 KB |
Output is correct |
7 |
Correct |
126 ms |
34932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
16180 KB |
Output is correct |
2 |
Correct |
176 ms |
43540 KB |
Output is correct |
3 |
Correct |
182 ms |
43944 KB |
Output is correct |
4 |
Correct |
174 ms |
42684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
16180 KB |
Output is correct |
2 |
Correct |
176 ms |
43540 KB |
Output is correct |
3 |
Correct |
182 ms |
43944 KB |
Output is correct |
4 |
Correct |
174 ms |
42684 KB |
Output is correct |
5 |
Correct |
43 ms |
16324 KB |
Output is correct |
6 |
Correct |
194 ms |
45692 KB |
Output is correct |
7 |
Correct |
166 ms |
46712 KB |
Output is correct |
8 |
Correct |
178 ms |
46600 KB |
Output is correct |
9 |
Correct |
192 ms |
46536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
16144 KB |
Output is correct |
2 |
Correct |
157 ms |
34440 KB |
Output is correct |
3 |
Correct |
168 ms |
34632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
16144 KB |
Output is correct |
2 |
Correct |
157 ms |
34440 KB |
Output is correct |
3 |
Correct |
168 ms |
34632 KB |
Output is correct |
4 |
Correct |
47 ms |
16244 KB |
Output is correct |
5 |
Correct |
163 ms |
36100 KB |
Output is correct |
6 |
Correct |
171 ms |
36516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16328 KB |
Output is correct |
2 |
Correct |
175 ms |
43480 KB |
Output is correct |
3 |
Correct |
177 ms |
43712 KB |
Output is correct |
4 |
Correct |
163 ms |
42480 KB |
Output is correct |
5 |
Correct |
47 ms |
16316 KB |
Output is correct |
6 |
Correct |
157 ms |
34408 KB |
Output is correct |
7 |
Correct |
170 ms |
34628 KB |
Output is correct |
8 |
Correct |
195 ms |
35004 KB |
Output is correct |
9 |
Correct |
199 ms |
35008 KB |
Output is correct |
10 |
Correct |
202 ms |
38848 KB |
Output is correct |
11 |
Correct |
220 ms |
37824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16328 KB |
Output is correct |
2 |
Correct |
175 ms |
43480 KB |
Output is correct |
3 |
Correct |
177 ms |
43712 KB |
Output is correct |
4 |
Correct |
163 ms |
42480 KB |
Output is correct |
5 |
Correct |
47 ms |
16316 KB |
Output is correct |
6 |
Correct |
157 ms |
34408 KB |
Output is correct |
7 |
Correct |
170 ms |
34628 KB |
Output is correct |
8 |
Correct |
195 ms |
35004 KB |
Output is correct |
9 |
Correct |
199 ms |
35008 KB |
Output is correct |
10 |
Correct |
202 ms |
38848 KB |
Output is correct |
11 |
Correct |
220 ms |
37824 KB |
Output is correct |
12 |
Correct |
42 ms |
16232 KB |
Output is correct |
13 |
Correct |
185 ms |
45688 KB |
Output is correct |
14 |
Correct |
168 ms |
46672 KB |
Output is correct |
15 |
Correct |
176 ms |
46468 KB |
Output is correct |
16 |
Correct |
179 ms |
46536 KB |
Output is correct |
17 |
Correct |
54 ms |
16416 KB |
Output is correct |
18 |
Correct |
162 ms |
36220 KB |
Output is correct |
19 |
Correct |
173 ms |
36472 KB |
Output is correct |
20 |
Correct |
194 ms |
36732 KB |
Output is correct |
21 |
Correct |
189 ms |
36876 KB |
Output is correct |
22 |
Correct |
201 ms |
39904 KB |
Output is correct |
23 |
Correct |
216 ms |
40456 KB |
Output is correct |
24 |
Correct |
208 ms |
39688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
16360 KB |
Output is correct |
2 |
Correct |
67 ms |
17096 KB |
Output is correct |
3 |
Correct |
69 ms |
17364 KB |
Output is correct |
4 |
Correct |
70 ms |
17340 KB |
Output is correct |
5 |
Correct |
64 ms |
17476 KB |
Output is correct |
6 |
Correct |
62 ms |
17096 KB |
Output is correct |
7 |
Correct |
47 ms |
16368 KB |
Output is correct |
8 |
Correct |
151 ms |
33584 KB |
Output is correct |
9 |
Correct |
153 ms |
33588 KB |
Output is correct |
10 |
Correct |
42 ms |
16324 KB |
Output is correct |
11 |
Correct |
166 ms |
43648 KB |
Output is correct |
12 |
Correct |
168 ms |
43740 KB |
Output is correct |
13 |
Correct |
159 ms |
42548 KB |
Output is correct |
14 |
Correct |
48 ms |
16172 KB |
Output is correct |
15 |
Correct |
162 ms |
34468 KB |
Output is correct |
16 |
Correct |
166 ms |
34796 KB |
Output is correct |
17 |
Correct |
194 ms |
34884 KB |
Output is correct |
18 |
Correct |
182 ms |
35032 KB |
Output is correct |
19 |
Correct |
200 ms |
38848 KB |
Output is correct |
20 |
Correct |
209 ms |
37932 KB |
Output is correct |
21 |
Correct |
171 ms |
34504 KB |
Output is correct |
22 |
Correct |
167 ms |
33980 KB |
Output is correct |
23 |
Correct |
172 ms |
34240 KB |
Output is correct |
24 |
Correct |
179 ms |
34616 KB |
Output is correct |
25 |
Correct |
202 ms |
36600 KB |
Output is correct |
26 |
Correct |
182 ms |
35008 KB |
Output is correct |
27 |
Correct |
195 ms |
35204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
16360 KB |
Output is correct |
2 |
Correct |
67 ms |
17096 KB |
Output is correct |
3 |
Correct |
69 ms |
17364 KB |
Output is correct |
4 |
Correct |
70 ms |
17340 KB |
Output is correct |
5 |
Correct |
64 ms |
17476 KB |
Output is correct |
6 |
Correct |
62 ms |
17096 KB |
Output is correct |
7 |
Correct |
47 ms |
16368 KB |
Output is correct |
8 |
Correct |
151 ms |
33584 KB |
Output is correct |
9 |
Correct |
153 ms |
33588 KB |
Output is correct |
10 |
Correct |
42 ms |
16324 KB |
Output is correct |
11 |
Correct |
166 ms |
43648 KB |
Output is correct |
12 |
Correct |
168 ms |
43740 KB |
Output is correct |
13 |
Correct |
159 ms |
42548 KB |
Output is correct |
14 |
Correct |
48 ms |
16172 KB |
Output is correct |
15 |
Correct |
162 ms |
34468 KB |
Output is correct |
16 |
Correct |
166 ms |
34796 KB |
Output is correct |
17 |
Correct |
194 ms |
34884 KB |
Output is correct |
18 |
Correct |
182 ms |
35032 KB |
Output is correct |
19 |
Correct |
200 ms |
38848 KB |
Output is correct |
20 |
Correct |
209 ms |
37932 KB |
Output is correct |
21 |
Correct |
171 ms |
34504 KB |
Output is correct |
22 |
Correct |
167 ms |
33980 KB |
Output is correct |
23 |
Correct |
172 ms |
34240 KB |
Output is correct |
24 |
Correct |
179 ms |
34616 KB |
Output is correct |
25 |
Correct |
202 ms |
36600 KB |
Output is correct |
26 |
Correct |
182 ms |
35008 KB |
Output is correct |
27 |
Correct |
195 ms |
35204 KB |
Output is correct |
28 |
Correct |
48 ms |
16320 KB |
Output is correct |
29 |
Correct |
57 ms |
18100 KB |
Output is correct |
30 |
Correct |
54 ms |
18072 KB |
Output is correct |
31 |
Correct |
69 ms |
18396 KB |
Output is correct |
32 |
Correct |
57 ms |
18216 KB |
Output is correct |
33 |
Correct |
55 ms |
17896 KB |
Output is correct |
34 |
Correct |
47 ms |
16388 KB |
Output is correct |
35 |
Correct |
146 ms |
34092 KB |
Output is correct |
36 |
Correct |
130 ms |
34676 KB |
Output is correct |
37 |
Correct |
120 ms |
34936 KB |
Output is correct |
38 |
Correct |
42 ms |
16320 KB |
Output is correct |
39 |
Correct |
192 ms |
45688 KB |
Output is correct |
40 |
Correct |
164 ms |
46712 KB |
Output is correct |
41 |
Correct |
176 ms |
46592 KB |
Output is correct |
42 |
Correct |
186 ms |
46932 KB |
Output is correct |
43 |
Correct |
47 ms |
16316 KB |
Output is correct |
44 |
Correct |
160 ms |
36092 KB |
Output is correct |
45 |
Correct |
177 ms |
36636 KB |
Output is correct |
46 |
Correct |
193 ms |
36728 KB |
Output is correct |
47 |
Correct |
185 ms |
36856 KB |
Output is correct |
48 |
Correct |
213 ms |
40116 KB |
Output is correct |
49 |
Correct |
204 ms |
40440 KB |
Output is correct |
50 |
Correct |
199 ms |
39356 KB |
Output is correct |
51 |
Correct |
140 ms |
35724 KB |
Output is correct |
52 |
Correct |
135 ms |
35956 KB |
Output is correct |
53 |
Correct |
127 ms |
35176 KB |
Output is correct |
54 |
Correct |
154 ms |
35416 KB |
Output is correct |
55 |
Correct |
135 ms |
35416 KB |
Output is correct |
56 |
Correct |
169 ms |
35024 KB |
Output is correct |
57 |
Correct |
171 ms |
37416 KB |
Output is correct |
58 |
Correct |
190 ms |
37176 KB |
Output is correct |
59 |
Correct |
182 ms |
35484 KB |
Output is correct |