#include <bits/stdc++.h>
using namespace std;
const int MAXN = 120005;
vector <pair <int, int>> adj[MAXN];
pair <int, int> queries[MAXN];
int t[MAXN];
int cnt = 0;
vector <int> pp[MAXN];
int dp[17][MAXN];
int u[17][MAXN];
int dep[MAXN];
void calc (int pos, int par) {
for (auto j : adj[pos]) {
if (j.first != par) {
dep[j.first] = 1 + dep[pos];
dp[0][j.first] = pos;
u[0][j.first] = j.second;
calc(j.first, pos);
}
}
}
int jump (int a, int b) {
for (int i = 16; 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 = 16; 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];
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
int l = 0;
for (int i = 1; i <= n + q - 1; i++) {
char x;
cin >> x;
if (x == 'S') {
cnt++;
int a, b;
cin >> a >> b;
adj[a].push_back({b, cnt});
adj[b].push_back({a, cnt});
} else {
t[++l] = cnt;
int a, b;
cin >> a >> b;
queries[l] = {a, b};
}
}
calc(1, 0);
for (int i = 1; i < 17; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
for (int i = 1; i <= l; i++) {
vector <int> pp;
int x = lca(queries[i].first, queries[i].second);
assert(x >= 1 && x <= n);
int a = queries[i].first, b = queries[i].second;
swap(a, b);
vector <int> d;
while (a != x) {
d.push_back(u[0][a]);
a = dp[0][a];
assert(a);
}
vector <int> g;
while (b != x) {
g.push_back(u[0][b]);
b = dp[0][b];
assert(b);
}
reverse(g.begin(), g.end());
for (auto j : g) d.push_back(j);
bool flag = 1;
for (int j = 1; j < (int)d.size(); j++) flag &= d[j] >= d[j - 1];
flag &= (d.back() <= t[i]);
cout << (flag ? "yes\n" : "no\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
17756 KB |
Output is correct |
2 |
Runtime error |
41 ms |
35684 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
17756 KB |
Output is correct |
2 |
Runtime error |
41 ms |
35684 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
17980 KB |
Output is correct |
2 |
Runtime error |
67 ms |
45108 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
17980 KB |
Output is correct |
2 |
Runtime error |
67 ms |
45108 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
17884 KB |
Output is correct |
2 |
Runtime error |
80 ms |
58472 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
17884 KB |
Output is correct |
2 |
Runtime error |
80 ms |
58472 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
17848 KB |
Output is correct |
2 |
Runtime error |
62 ms |
45136 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
17848 KB |
Output is correct |
2 |
Runtime error |
62 ms |
45136 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
17752 KB |
Output is correct |
2 |
Runtime error |
98 ms |
58512 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
17752 KB |
Output is correct |
2 |
Runtime error |
98 ms |
58512 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
17756 KB |
Output is correct |
2 |
Runtime error |
46 ms |
35684 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
17756 KB |
Output is correct |
2 |
Runtime error |
46 ms |
35684 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |