#include <bits/stdc++.h>
using namespace std;
const int N = 120'000 + 10,
MAX = 1'000'000;
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] < increase[f[u][i - 1]][0])
increase[u][i] = increase[f[u][i - 1]][i - 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 = MAX, ma = 0;
for (int i = 17; i >= 0; --i) {
if (anc(f[u][i], v)) continue;
if (uwd >= increase[u][0]) return MAX;
uwd = increase[u][i];
u = f[u][i];
}
if (!anc(u, v)) {
if (uwd >= increase[u][0]) return MAX;
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]) return MAX;
dwd = decrease[v][i];
ma = max(ma, decrease[v][0]);
v = f[v][i];
}
if (v != u) {
if (dwd <= decrease[v][0]) return MAX;
dwd = decrease[v][0];
ma = max(ma, decrease[v][0]);
v = f[v][0];
}
return uwd < dwd ? ma : MAX;
}
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, 63, sizeof increase);
dfs(f[1][0] = 1);
for (const auto& [a, d, i] : Q) {
if (!a) continue;
int time = get(d, a);
cout << (time <= i ? "yes" : "no") << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15408 KB |
Output is correct |
2 |
Correct |
31 ms |
15568 KB |
Output is correct |
3 |
Correct |
51 ms |
20448 KB |
Output is correct |
4 |
Correct |
27 ms |
20460 KB |
Output is correct |
5 |
Correct |
30 ms |
18960 KB |
Output is correct |
6 |
Correct |
30 ms |
20380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15408 KB |
Output is correct |
2 |
Correct |
31 ms |
15568 KB |
Output is correct |
3 |
Correct |
51 ms |
20448 KB |
Output is correct |
4 |
Correct |
27 ms |
20460 KB |
Output is correct |
5 |
Correct |
30 ms |
18960 KB |
Output is correct |
6 |
Correct |
30 ms |
20380 KB |
Output is correct |
7 |
Incorrect |
24 ms |
17776 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
16844 KB |
Output is correct |
2 |
Correct |
90 ms |
35944 KB |
Output is correct |
3 |
Correct |
94 ms |
35956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
16844 KB |
Output is correct |
2 |
Correct |
90 ms |
35944 KB |
Output is correct |
3 |
Correct |
94 ms |
35956 KB |
Output is correct |
4 |
Incorrect |
21 ms |
16896 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
16860 KB |
Output is correct |
2 |
Correct |
106 ms |
44548 KB |
Output is correct |
3 |
Correct |
88 ms |
47888 KB |
Output is correct |
4 |
Correct |
88 ms |
47616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
16860 KB |
Output is correct |
2 |
Correct |
106 ms |
44548 KB |
Output is correct |
3 |
Correct |
88 ms |
47888 KB |
Output is correct |
4 |
Correct |
88 ms |
47616 KB |
Output is correct |
5 |
Incorrect |
21 ms |
17612 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
16844 KB |
Output is correct |
2 |
Correct |
77 ms |
35848 KB |
Output is correct |
3 |
Correct |
93 ms |
39840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
16844 KB |
Output is correct |
2 |
Correct |
77 ms |
35848 KB |
Output is correct |
3 |
Correct |
93 ms |
39840 KB |
Output is correct |
4 |
Incorrect |
20 ms |
17616 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16844 KB |
Output is correct |
2 |
Correct |
89 ms |
44316 KB |
Output is correct |
3 |
Correct |
88 ms |
47876 KB |
Output is correct |
4 |
Correct |
88 ms |
47616 KB |
Output is correct |
5 |
Correct |
20 ms |
17612 KB |
Output is correct |
6 |
Correct |
63 ms |
39172 KB |
Output is correct |
7 |
Correct |
80 ms |
39680 KB |
Output is correct |
8 |
Correct |
89 ms |
40116 KB |
Output is correct |
9 |
Correct |
79 ms |
40192 KB |
Output is correct |
10 |
Correct |
93 ms |
44736 KB |
Output is correct |
11 |
Correct |
99 ms |
44036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16844 KB |
Output is correct |
2 |
Correct |
89 ms |
44316 KB |
Output is correct |
3 |
Correct |
88 ms |
47876 KB |
Output is correct |
4 |
Correct |
88 ms |
47616 KB |
Output is correct |
5 |
Correct |
20 ms |
17612 KB |
Output is correct |
6 |
Correct |
63 ms |
39172 KB |
Output is correct |
7 |
Correct |
80 ms |
39680 KB |
Output is correct |
8 |
Correct |
89 ms |
40116 KB |
Output is correct |
9 |
Correct |
79 ms |
40192 KB |
Output is correct |
10 |
Correct |
93 ms |
44736 KB |
Output is correct |
11 |
Correct |
99 ms |
44036 KB |
Output is correct |
12 |
Incorrect |
23 ms |
17612 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16844 KB |
Output is correct |
2 |
Correct |
27 ms |
19176 KB |
Output is correct |
3 |
Correct |
25 ms |
20680 KB |
Output is correct |
4 |
Correct |
27 ms |
20440 KB |
Output is correct |
5 |
Correct |
29 ms |
18860 KB |
Output is correct |
6 |
Correct |
27 ms |
20480 KB |
Output is correct |
7 |
Correct |
22 ms |
17872 KB |
Output is correct |
8 |
Correct |
70 ms |
38772 KB |
Output is correct |
9 |
Correct |
72 ms |
38752 KB |
Output is correct |
10 |
Correct |
20 ms |
17612 KB |
Output is correct |
11 |
Correct |
89 ms |
47804 KB |
Output is correct |
12 |
Correct |
84 ms |
47880 KB |
Output is correct |
13 |
Correct |
83 ms |
47620 KB |
Output is correct |
14 |
Correct |
21 ms |
17616 KB |
Output is correct |
15 |
Correct |
74 ms |
39352 KB |
Output is correct |
16 |
Correct |
81 ms |
39680 KB |
Output is correct |
17 |
Correct |
79 ms |
40196 KB |
Output is correct |
18 |
Correct |
80 ms |
40196 KB |
Output is correct |
19 |
Correct |
93 ms |
44800 KB |
Output is correct |
20 |
Correct |
114 ms |
44032 KB |
Output is correct |
21 |
Correct |
71 ms |
39348 KB |
Output is correct |
22 |
Correct |
73 ms |
39432 KB |
Output is correct |
23 |
Correct |
104 ms |
39612 KB |
Output is correct |
24 |
Correct |
82 ms |
39684 KB |
Output is correct |
25 |
Correct |
94 ms |
41588 KB |
Output is correct |
26 |
Correct |
78 ms |
39272 KB |
Output is correct |
27 |
Correct |
87 ms |
39420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16844 KB |
Output is correct |
2 |
Correct |
27 ms |
19176 KB |
Output is correct |
3 |
Correct |
25 ms |
20680 KB |
Output is correct |
4 |
Correct |
27 ms |
20440 KB |
Output is correct |
5 |
Correct |
29 ms |
18860 KB |
Output is correct |
6 |
Correct |
27 ms |
20480 KB |
Output is correct |
7 |
Correct |
22 ms |
17872 KB |
Output is correct |
8 |
Correct |
70 ms |
38772 KB |
Output is correct |
9 |
Correct |
72 ms |
38752 KB |
Output is correct |
10 |
Correct |
20 ms |
17612 KB |
Output is correct |
11 |
Correct |
89 ms |
47804 KB |
Output is correct |
12 |
Correct |
84 ms |
47880 KB |
Output is correct |
13 |
Correct |
83 ms |
47620 KB |
Output is correct |
14 |
Correct |
21 ms |
17616 KB |
Output is correct |
15 |
Correct |
74 ms |
39352 KB |
Output is correct |
16 |
Correct |
81 ms |
39680 KB |
Output is correct |
17 |
Correct |
79 ms |
40196 KB |
Output is correct |
18 |
Correct |
80 ms |
40196 KB |
Output is correct |
19 |
Correct |
93 ms |
44800 KB |
Output is correct |
20 |
Correct |
114 ms |
44032 KB |
Output is correct |
21 |
Correct |
71 ms |
39348 KB |
Output is correct |
22 |
Correct |
73 ms |
39432 KB |
Output is correct |
23 |
Correct |
104 ms |
39612 KB |
Output is correct |
24 |
Correct |
82 ms |
39684 KB |
Output is correct |
25 |
Correct |
94 ms |
41588 KB |
Output is correct |
26 |
Correct |
78 ms |
39272 KB |
Output is correct |
27 |
Correct |
87 ms |
39420 KB |
Output is correct |
28 |
Incorrect |
21 ms |
17612 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |