#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
const int MAX_N = 2e5;
const int infinity = 1e9;
vii tree[MAX_N];
int depth[MAX_N];
pii dp[20][MAX_N];// weight[MAX_N];
int increase[MAX_N], decrease[MAX_N];
int loga[MAX_N];
void Dfs(int node, int parent, int w, int len_i, int len_d, int dist) {
depth[node] = dist;
dp[0][node] = {parent, w};
// weight[node] = w;
increase[node] = len_i, decrease[node] = len_d;
for (auto [neighbor, d] : tree[node]) {
if (neighbor == parent) continue;
Dfs(neighbor, node, d, (w > d ? len_i : 0) + 1, (w < d ? len_d : 0) + 1, dist + 1);
}
}
bool Dfs(int node, int end, int w) {
if (node == end) return true;
for (auto [neighbor, d] : tree[node]) {
if (d >= w) continue;
if (Dfs(neighbor, end, d)) {
return true;
}
}
return false;
}
int Dfs(int node, int w) {
int c = 1;
for (auto [neighbor, d] : tree[node]) {
if (d <= w) continue;
c += Dfs(neighbor, d);
}
return c;
}
int n, q;
pii Jump(int a, int k) {
int max_w = 0;
for (int i = 0; i <= loga[n]; i++) {
if ((k >> i) & 1) {
chkmax(max_w, dp[i][a].y);
a = dp[i][a].x;
}
}
return {a, max_w};
}
int Solve(int v, int u) {
int a = v, b = u;
int max_w = 0;
if (depth[a] > depth[b]) {
pii p = Jump(a, depth[a] - depth[b]);
a = p.x, max_w = p.y;
} else {
pii p = Jump(b, depth[b] - depth[a]);
b = p.x, max_w = p.y;
}
bool bad = a != b;
for (int i = loga[n]; i >= 0; i--) {
if (dp[i][a] != dp[i][b]) {
chkmax(max_w, dp[i][a].y);
chkmax(max_w, dp[i][b].y);
a = dp[i][a].x, b = dp[i][b].x;
}
}
int dep_lca = depth[a] - bad;
if (bad) {
chkmax(max_w, dp[0][a].y);
chkmax(max_w, dp[0][b].y);
}
if (dep_lca < depth[v] - decrease[v] || dep_lca < depth[u] - increase[u]) {
return infinity;
}
return (!bad || dp[0][a].y > dp[0][b].y ? max_w : infinity);
}
char type[MAX_N];
int a[MAX_N], d[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n + q - 1; i++) {
cin >> type[i];
if (type[i] == 'S') {
int A, b;
cin >> A >> b;
tree[A].push_back({b, i});
tree[b].push_back({A, i});
} else if (type[i] == 'Q') {
// int a, d;
cin >> a[i] >> d[i];
// cout << (Dfs(a, d, infinity) ? "yes\n" : "no\n");
} else {
// int d;
cin >> d[i];
// cout << Dfs(d, -1) << '\n';
}
}
for (int i = 2; i <= n; i++) {
loga[i] = loga[i >> 1] + 1;
}
Dfs(1, 0, 0, 0, 0, 0);
for (int i = 1; i <= loga[n]; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j].x = dp[i - 1][dp[i - 1][j].x].x;
dp[i][j].y = max(dp[i - 1][j].y, dp[i - 1][dp[i - 1][j].x].y);
}
}
for (int i = 0; i < n + q - 1; i++) {
if (type[i] == 'Q') {
cout << (Solve(a[i], d[i]) <= i ? "yes\n" : "no\n");
} else if (type[i] == 'C') {
cout << Dfs(d[i], -1) << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
6424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
6424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6452 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6452 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6392 KB |
Output is correct |
2 |
Runtime error |
128 ms |
90768 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6392 KB |
Output is correct |
2 |
Runtime error |
128 ms |
90768 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6484 KB |
Output is correct |
2 |
Runtime error |
129 ms |
90780 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6484 KB |
Output is correct |
2 |
Runtime error |
129 ms |
90780 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |