#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] < increase[f[u][i - 1]][i - 1])
increase[u][i] = increase[f[u][i - 1]][i - 1];
if (decrease[u][i - 1] > decrease[f[u][i - 1]][i - 1])
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 = N, ma = 0;
for (int i = 17; i >= 0; --i) {
if (anc(f[u][i], v)) continue;
if (uwd >= increase[u][0]) return N;
uwd = increase[u][i];
u = f[u][i];
}
if (!anc(u, v)) {
if (uwd >= increase[u][0]) return N;
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 N;
dwd = decrease[v][i];
ma = max(ma, decrease[v][0]);
v = f[v][i];
}
if (v != u) {
if (dwd <= decrease[v][0]) return N;
dwd = decrease[v][0];
ma = max(ma, decrease[v][0]);
v = f[v][0];
}
return uwd < dwd ? ma : N;
}
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);
memset(decrease, 63, sizeof decrease);
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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
26064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
26064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
26128 KB |
Output is correct |
2 |
Incorrect |
104 ms |
38804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
26128 KB |
Output is correct |
2 |
Incorrect |
104 ms |
38804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
26056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
26056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
26048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
26048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
26156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
26156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
26196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
26196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |