#include <bits/stdc++.h>
using namespace std;
const int N = 120'000 + 10;
int n, k;
vector<pair<int, int>> ad[N];
vector<tuple<int, int, int>> Q;
int st[N], ed[N], it;
int f[N][18], increase[N][18], decrease[N][18];
void dfs(int u, int p = -1) {
st[u] = ++it;
for (int i = 1; i <= 17; ++i) {
f[u][i] = f[f[u][i - 1]][i - 1];
if (increase[u][i - 1] != -1 && increase[f[u][i - 1]][i - 1] != -1) {
if (increase[u][i - 1] < increase[f[u][i - 1]][0])
increase[u][i] = increase[f[u][i - 1]][i - 1];
}
if (decrease[u][i - 1] != -1 && decrease[f[u][i - 1]][i - 1] != -1) {
if (decrease[u][i - 1] > decrease[f[u][i - 1]][0])
decrease[u][i] = decrease[f[u][i - 1]][i - 1];
}
}
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
f[v][0] = u;
increase[v][0] = decrease[v][0] = w;
dfs(v, u);
}
ed[u] = it;
}
inline bool anc(int u, int v) { return st[u] <= st[v] && ed[u] >= ed[v]; }
int get(int u, int v) {
int uwd = 0, dwd = 1'000'000, ma = 0;
for (int i = 17; i >= 0; --i) {
if (anc(f[u][i], v)) continue;
if (uwd > increase[u][0] || increase[u][i] == -1) return -1;
uwd = increase[u][i];
u = f[u][i];
}
if (!anc(u, v)) {
if (uwd > increase[u][0] || increase[u][0] == -1) return -1;
uwd = increase[u][0];
u = f[u][0];
} ma = uwd;
for (int i = 17; i >= 0; --i) {
if (anc(f[v][i], u)) continue;
if (dwd < decrease[v][0] || decrease[v][i] == -1) return -1;
dwd = decrease[v][i];
ma = max(ma, decrease[v][0]);
v = f[v][i];
}
if (v != u) {
if (dwd < decrease[v][0] || decrease[v][0] == -1) return -1;
dwd = decrease[v][0];
ma = max(ma, decrease[v][0]);
v = f[v][0];
}
return uwd < dwd ? ma : -1;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i < n + k; ++i) {
char type; cin >> type;
if (type == 'S') {
int a, b; cin >> a >> b;
ad[a].push_back({b, i});
ad[b].push_back({a, i});
}
if (type == 'Q') {
int a, d; cin >> a >> d;
Q.emplace_back(a, d, i);
}
if (type == 'C') {
int d; cin >> d;
Q.emplace_back(0, d, i);
}
}
memset(increase, -1, sizeof increase);
memset(decrease, -1, sizeof decrease);
dfs(f[1][0] = 1);
for (const auto& [a, d, i] : Q) {
if (!a) continue;
int time = get(d, a);
cout << (time == -1 || time > i ? "no" : "yes") << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25288 KB |
Output is correct |
2 |
Correct |
27 ms |
25308 KB |
Output is correct |
3 |
Correct |
25 ms |
25300 KB |
Output is correct |
4 |
Correct |
25 ms |
25300 KB |
Output is correct |
5 |
Correct |
29 ms |
25616 KB |
Output is correct |
6 |
Correct |
29 ms |
25200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25288 KB |
Output is correct |
2 |
Correct |
27 ms |
25308 KB |
Output is correct |
3 |
Correct |
25 ms |
25300 KB |
Output is correct |
4 |
Correct |
25 ms |
25300 KB |
Output is correct |
5 |
Correct |
29 ms |
25616 KB |
Output is correct |
6 |
Correct |
29 ms |
25200 KB |
Output is correct |
7 |
Incorrect |
24 ms |
25284 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25292 KB |
Output is correct |
2 |
Correct |
69 ms |
35900 KB |
Output is correct |
3 |
Correct |
65 ms |
35960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25292 KB |
Output is correct |
2 |
Correct |
69 ms |
35900 KB |
Output is correct |
3 |
Correct |
65 ms |
35960 KB |
Output is correct |
4 |
Incorrect |
22 ms |
25284 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25284 KB |
Output is correct |
2 |
Correct |
83 ms |
44564 KB |
Output is correct |
3 |
Correct |
82 ms |
47872 KB |
Output is correct |
4 |
Correct |
85 ms |
47616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25284 KB |
Output is correct |
2 |
Correct |
83 ms |
44564 KB |
Output is correct |
3 |
Correct |
82 ms |
47872 KB |
Output is correct |
4 |
Correct |
85 ms |
47616 KB |
Output is correct |
5 |
Incorrect |
22 ms |
26052 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25292 KB |
Output is correct |
2 |
Correct |
79 ms |
35844 KB |
Output is correct |
3 |
Correct |
76 ms |
36512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25292 KB |
Output is correct |
2 |
Correct |
79 ms |
35844 KB |
Output is correct |
3 |
Correct |
76 ms |
36512 KB |
Output is correct |
4 |
Incorrect |
22 ms |
25288 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
25296 KB |
Output is correct |
2 |
Correct |
84 ms |
44420 KB |
Output is correct |
3 |
Correct |
86 ms |
47872 KB |
Output is correct |
4 |
Correct |
81 ms |
47620 KB |
Output is correct |
5 |
Correct |
21 ms |
26052 KB |
Output is correct |
6 |
Correct |
68 ms |
39228 KB |
Output is correct |
7 |
Correct |
94 ms |
39820 KB |
Output is correct |
8 |
Correct |
81 ms |
40192 KB |
Output is correct |
9 |
Correct |
85 ms |
40196 KB |
Output is correct |
10 |
Correct |
95 ms |
44804 KB |
Output is correct |
11 |
Correct |
118 ms |
44036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
25296 KB |
Output is correct |
2 |
Correct |
84 ms |
44420 KB |
Output is correct |
3 |
Correct |
86 ms |
47872 KB |
Output is correct |
4 |
Correct |
81 ms |
47620 KB |
Output is correct |
5 |
Correct |
21 ms |
26052 KB |
Output is correct |
6 |
Correct |
68 ms |
39228 KB |
Output is correct |
7 |
Correct |
94 ms |
39820 KB |
Output is correct |
8 |
Correct |
81 ms |
40192 KB |
Output is correct |
9 |
Correct |
85 ms |
40196 KB |
Output is correct |
10 |
Correct |
95 ms |
44804 KB |
Output is correct |
11 |
Correct |
118 ms |
44036 KB |
Output is correct |
12 |
Incorrect |
20 ms |
26056 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25308 KB |
Output is correct |
2 |
Correct |
25 ms |
25236 KB |
Output is correct |
3 |
Correct |
28 ms |
25316 KB |
Output is correct |
4 |
Correct |
25 ms |
25308 KB |
Output is correct |
5 |
Correct |
25 ms |
25612 KB |
Output is correct |
6 |
Correct |
29 ms |
25320 KB |
Output is correct |
7 |
Correct |
21 ms |
25296 KB |
Output is correct |
8 |
Correct |
63 ms |
35932 KB |
Output is correct |
9 |
Correct |
84 ms |
35960 KB |
Output is correct |
10 |
Correct |
21 ms |
25292 KB |
Output is correct |
11 |
Correct |
83 ms |
44320 KB |
Output is correct |
12 |
Correct |
87 ms |
47872 KB |
Output is correct |
13 |
Correct |
82 ms |
47620 KB |
Output is correct |
14 |
Correct |
21 ms |
26168 KB |
Output is correct |
15 |
Correct |
69 ms |
39424 KB |
Output is correct |
16 |
Correct |
81 ms |
39684 KB |
Output is correct |
17 |
Correct |
80 ms |
40192 KB |
Output is correct |
18 |
Correct |
80 ms |
40208 KB |
Output is correct |
19 |
Correct |
91 ms |
44792 KB |
Output is correct |
20 |
Correct |
95 ms |
43908 KB |
Output is correct |
21 |
Correct |
72 ms |
39252 KB |
Output is correct |
22 |
Correct |
67 ms |
39424 KB |
Output is correct |
23 |
Correct |
94 ms |
39644 KB |
Output is correct |
24 |
Correct |
75 ms |
40028 KB |
Output is correct |
25 |
Correct |
83 ms |
41592 KB |
Output is correct |
26 |
Correct |
78 ms |
39136 KB |
Output is correct |
27 |
Correct |
87 ms |
39364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25308 KB |
Output is correct |
2 |
Correct |
25 ms |
25236 KB |
Output is correct |
3 |
Correct |
28 ms |
25316 KB |
Output is correct |
4 |
Correct |
25 ms |
25308 KB |
Output is correct |
5 |
Correct |
25 ms |
25612 KB |
Output is correct |
6 |
Correct |
29 ms |
25320 KB |
Output is correct |
7 |
Correct |
21 ms |
25296 KB |
Output is correct |
8 |
Correct |
63 ms |
35932 KB |
Output is correct |
9 |
Correct |
84 ms |
35960 KB |
Output is correct |
10 |
Correct |
21 ms |
25292 KB |
Output is correct |
11 |
Correct |
83 ms |
44320 KB |
Output is correct |
12 |
Correct |
87 ms |
47872 KB |
Output is correct |
13 |
Correct |
82 ms |
47620 KB |
Output is correct |
14 |
Correct |
21 ms |
26168 KB |
Output is correct |
15 |
Correct |
69 ms |
39424 KB |
Output is correct |
16 |
Correct |
81 ms |
39684 KB |
Output is correct |
17 |
Correct |
80 ms |
40192 KB |
Output is correct |
18 |
Correct |
80 ms |
40208 KB |
Output is correct |
19 |
Correct |
91 ms |
44792 KB |
Output is correct |
20 |
Correct |
95 ms |
43908 KB |
Output is correct |
21 |
Correct |
72 ms |
39252 KB |
Output is correct |
22 |
Correct |
67 ms |
39424 KB |
Output is correct |
23 |
Correct |
94 ms |
39644 KB |
Output is correct |
24 |
Correct |
75 ms |
40028 KB |
Output is correct |
25 |
Correct |
83 ms |
41592 KB |
Output is correct |
26 |
Correct |
78 ms |
39136 KB |
Output is correct |
27 |
Correct |
87 ms |
39364 KB |
Output is correct |
28 |
Incorrect |
31 ms |
26064 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |