#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 25;
const int B = 18;
int n, q;
array <int, 4> queries[MAXN];
vector <pair <int, int>> adj[MAXN];
int p[MAXN], dp[B][MAXN], dep[MAXN], top[B][MAXN], inc[B][MAXN], decr[B][MAXN];
void dfs (int pos, int par) {
p[pos] = par;
for (auto [j, k] : adj[pos]) {
if (j == par) continue;
dep[j] = 1 + dep[pos];
dp[0][j] = pos;
top[0][j] = k;
inc[0][j] = 1;
decr[0][j] = 1;
for (int i = 1; i < B; i++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
top[i][j] = top[i - 1][dp[i - 1][j]];
inc[i][j] = inc[i - 1][j] && inc[i - 1][dp[i - 1][j]] && (top[i - 1][j] <= top[0][dp[i - 1][j]]);
decr[i][j] = decr[i - 1][j] && decr[i - 1][dp[i - 1][j]] && (top[i - 1][j] >= top[0][dp[i - 1][j]]);
}
dfs(j, pos);
}
}
int jump (int a, int b) {
for (int i = B - 1; i >= 0; i--) {
if (b & (1 << i)) {
a = dp[i][a];
}
}
return a;
}
int lca (int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
b = jump(b, dep[b] - dep[a]);
if (a == b) return a;
for (int i = B - 1; i >= 0; i--) {
int x = dp[i][a], y = dp[i][b];
if (x && y && x != y) {
a = x, b = y;
}
}
return dp[0][a];
}
pair <bool, int> check_inc (int a, int b, int x) {
bool f = 1; int last = 0;
for (int i = B - 1; i >= 0; i--) {
int z = dp[i][a];
if (!z || dep[z] <= dep[b]) continue;
f &= inc[i][a]; f &= top[0][a] >= last;
last = top[i][a]; a = z;
}
f &= top[0][a] <= x;
f &= top[0][a] >= last;
return {f, top[0][a]};
}
pair <bool, int> check_decr (int a, int b, int x) {
bool f = top[0][a] <= x; int last = 1e9;
for (int i = B - 1; i >= 0; i--) {
int z = dp[i][a];
if (!z || dep[z] <= dep[b]) continue;
f &= decr[i][a]; f &= top[0][a] <= last;
last = top[i][a]; a = z;
}
f &= top[0][a] <= last;
return {f, top[0][a]};
}
bool check (int a, int b, int x) {
if (a == b) return 1;
int z = lca(a, b);
if (z == a) return check_decr(b, z, x).first;
if (z == b) return check_inc(a, z, x).first;
auto g = check_inc(a, z, x), h = check_decr(b, z, x);
return g.first && h.first && g.second <= h.second;
}
void solve () {
cin >> n >> q;
int ee = 0;
for (int i = 1; i <= n - 1 + q; i++) {
char c; cin >> c;
if (c == 'S') {
int x, y; cin >> x >> y;
adj[x].push_back({y, i});
adj[y].push_back({x, i});
} else if (c == 'Q') {
int x, y; cin >> x >> y;
queries[++ee] = {1, y, x, i};
} else {
int x; cin >> x;
queries[++ee] = {2, x, 0, i};
}
}
dfs(1, 0);
for (int i = 1; i <= q; i++) {
auto g = check(queries[i][1], queries[i][2], queries[i][3]);
cout << (g ? "yes\n" : "no\n");
}
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
Compilation message
servers.cpp: In function 'void dfs(int, int)':
servers.cpp:11:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | for (auto [j, k] : adj[pos]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
95056 KB |
Output is correct |
2 |
Correct |
44 ms |
96636 KB |
Output is correct |
3 |
Correct |
35 ms |
96856 KB |
Output is correct |
4 |
Correct |
56 ms |
96848 KB |
Output is correct |
5 |
Correct |
46 ms |
97184 KB |
Output is correct |
6 |
Correct |
33 ms |
96812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
95056 KB |
Output is correct |
2 |
Correct |
44 ms |
96636 KB |
Output is correct |
3 |
Correct |
35 ms |
96856 KB |
Output is correct |
4 |
Correct |
56 ms |
96848 KB |
Output is correct |
5 |
Correct |
46 ms |
97184 KB |
Output is correct |
6 |
Correct |
33 ms |
96812 KB |
Output is correct |
7 |
Incorrect |
31 ms |
96080 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
95184 KB |
Output is correct |
2 |
Correct |
104 ms |
103768 KB |
Output is correct |
3 |
Correct |
95 ms |
103648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
95184 KB |
Output is correct |
2 |
Correct |
104 ms |
103768 KB |
Output is correct |
3 |
Correct |
95 ms |
103648 KB |
Output is correct |
4 |
Incorrect |
29 ms |
96080 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
95064 KB |
Output is correct |
2 |
Correct |
92 ms |
116304 KB |
Output is correct |
3 |
Correct |
98 ms |
116564 KB |
Output is correct |
4 |
Correct |
110 ms |
116240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
95064 KB |
Output is correct |
2 |
Correct |
92 ms |
116304 KB |
Output is correct |
3 |
Correct |
98 ms |
116564 KB |
Output is correct |
4 |
Correct |
110 ms |
116240 KB |
Output is correct |
5 |
Incorrect |
25 ms |
96084 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
95056 KB |
Output is correct |
2 |
Correct |
84 ms |
104236 KB |
Output is correct |
3 |
Correct |
93 ms |
104528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
95056 KB |
Output is correct |
2 |
Correct |
84 ms |
104236 KB |
Output is correct |
3 |
Correct |
93 ms |
104528 KB |
Output is correct |
4 |
Incorrect |
29 ms |
96092 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
95012 KB |
Output is correct |
2 |
Correct |
99 ms |
116328 KB |
Output is correct |
3 |
Correct |
110 ms |
116308 KB |
Output is correct |
4 |
Correct |
120 ms |
116256 KB |
Output is correct |
5 |
Correct |
30 ms |
96088 KB |
Output is correct |
6 |
Correct |
82 ms |
104088 KB |
Output is correct |
7 |
Correct |
98 ms |
104524 KB |
Output is correct |
8 |
Correct |
152 ms |
105296 KB |
Output is correct |
9 |
Correct |
113 ms |
104956 KB |
Output is correct |
10 |
Correct |
126 ms |
110928 KB |
Output is correct |
11 |
Correct |
172 ms |
109904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
95012 KB |
Output is correct |
2 |
Correct |
99 ms |
116328 KB |
Output is correct |
3 |
Correct |
110 ms |
116308 KB |
Output is correct |
4 |
Correct |
120 ms |
116256 KB |
Output is correct |
5 |
Correct |
30 ms |
96088 KB |
Output is correct |
6 |
Correct |
82 ms |
104088 KB |
Output is correct |
7 |
Correct |
98 ms |
104524 KB |
Output is correct |
8 |
Correct |
152 ms |
105296 KB |
Output is correct |
9 |
Correct |
113 ms |
104956 KB |
Output is correct |
10 |
Correct |
126 ms |
110928 KB |
Output is correct |
11 |
Correct |
172 ms |
109904 KB |
Output is correct |
12 |
Incorrect |
25 ms |
96092 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
95208 KB |
Output is correct |
2 |
Correct |
42 ms |
96752 KB |
Output is correct |
3 |
Correct |
36 ms |
96848 KB |
Output is correct |
4 |
Correct |
61 ms |
96920 KB |
Output is correct |
5 |
Correct |
48 ms |
97148 KB |
Output is correct |
6 |
Correct |
34 ms |
96860 KB |
Output is correct |
7 |
Correct |
35 ms |
96084 KB |
Output is correct |
8 |
Correct |
102 ms |
103816 KB |
Output is correct |
9 |
Correct |
93 ms |
103628 KB |
Output is correct |
10 |
Correct |
27 ms |
96080 KB |
Output is correct |
11 |
Correct |
106 ms |
116460 KB |
Output is correct |
12 |
Correct |
104 ms |
116452 KB |
Output is correct |
13 |
Correct |
120 ms |
116308 KB |
Output is correct |
14 |
Correct |
29 ms |
96080 KB |
Output is correct |
15 |
Correct |
111 ms |
104064 KB |
Output is correct |
16 |
Correct |
87 ms |
104728 KB |
Output is correct |
17 |
Correct |
150 ms |
105112 KB |
Output is correct |
18 |
Correct |
111 ms |
105040 KB |
Output is correct |
19 |
Correct |
125 ms |
110940 KB |
Output is correct |
20 |
Correct |
161 ms |
109964 KB |
Output is correct |
21 |
Correct |
91 ms |
104260 KB |
Output is correct |
22 |
Correct |
92 ms |
104376 KB |
Output is correct |
23 |
Correct |
103 ms |
104528 KB |
Output is correct |
24 |
Correct |
106 ms |
104528 KB |
Output is correct |
25 |
Correct |
117 ms |
107372 KB |
Output is correct |
26 |
Correct |
109 ms |
104156 KB |
Output is correct |
27 |
Correct |
109 ms |
104184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
95208 KB |
Output is correct |
2 |
Correct |
42 ms |
96752 KB |
Output is correct |
3 |
Correct |
36 ms |
96848 KB |
Output is correct |
4 |
Correct |
61 ms |
96920 KB |
Output is correct |
5 |
Correct |
48 ms |
97148 KB |
Output is correct |
6 |
Correct |
34 ms |
96860 KB |
Output is correct |
7 |
Correct |
35 ms |
96084 KB |
Output is correct |
8 |
Correct |
102 ms |
103816 KB |
Output is correct |
9 |
Correct |
93 ms |
103628 KB |
Output is correct |
10 |
Correct |
27 ms |
96080 KB |
Output is correct |
11 |
Correct |
106 ms |
116460 KB |
Output is correct |
12 |
Correct |
104 ms |
116452 KB |
Output is correct |
13 |
Correct |
120 ms |
116308 KB |
Output is correct |
14 |
Correct |
29 ms |
96080 KB |
Output is correct |
15 |
Correct |
111 ms |
104064 KB |
Output is correct |
16 |
Correct |
87 ms |
104728 KB |
Output is correct |
17 |
Correct |
150 ms |
105112 KB |
Output is correct |
18 |
Correct |
111 ms |
105040 KB |
Output is correct |
19 |
Correct |
125 ms |
110940 KB |
Output is correct |
20 |
Correct |
161 ms |
109964 KB |
Output is correct |
21 |
Correct |
91 ms |
104260 KB |
Output is correct |
22 |
Correct |
92 ms |
104376 KB |
Output is correct |
23 |
Correct |
103 ms |
104528 KB |
Output is correct |
24 |
Correct |
106 ms |
104528 KB |
Output is correct |
25 |
Correct |
117 ms |
107372 KB |
Output is correct |
26 |
Correct |
109 ms |
104156 KB |
Output is correct |
27 |
Correct |
109 ms |
104184 KB |
Output is correct |
28 |
Incorrect |
30 ms |
96084 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |