#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 100;
tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> os;
vector<vector<pair<int,int>>> graph;
vector<array<int,3>> query;
vector<int> query_ids[MAXN];
int N, K, sz[MAXN], ans[MAXN], marked[MAXN], color[MAXN];
int ttt[MAXN], out_smallest[MAXN], out_greatest[MAXN], in_greatest[MAXN];
int get_sz(int node, int parent) {
sz[node] = 1;
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
sz[node] += get_sz(u.f, node);
}
return sz[node];
}
int find_centro(int node, int parent, int n) {
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f] && sz[u.f] * 2 >= n)
return find_centro(u.f, node, n);
}
return node;
}
vector<int> V;
void dfs(int node, int parent, int minimum_edge, int last_edge) {
out_smallest[node] = minimum_edge;
out_greatest[node] = last_edge;
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f] && u.s > last_edge)
dfs(u.f, node, minimum_edge, u.s);
}
}
void dfs2(int node, int parent, int minimum_edge, int maximum_edge) {
in_greatest[node] = maximum_edge;
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f] && u.s < minimum_edge)
dfs2(u.f, node, u.s, maximum_edge);
}
}
void init(int node, int parent, int cc) {
V.push_back(node);
color[node] = cc;
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
init(u.f, node, cc);
}
}
void activate(int node, int parent) {
if (in_greatest[node] != -1)
os.insert({in_greatest[node], node});
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
activate(u.f, node);
}
}
void decompose(int node) {
int n = get_sz(node, node);
int centro = find_centro(node, node, n);
marked[centro] = true;
int cc = 0;
os.clear();
for (auto u : graph[centro]) {
if (!marked[u.f]) {
init(u.f, centro, ++cc);
dfs(u.f, centro, u.s, u.s);
dfs2(u.f, centro, u.s, u.s);
activate(u.f, centro);
}
}
for (auto id : query_ids[centro]) {
array<int,3> Q = query[id];
if (Q[0] == centro)
ans[Q[2]] += (in_greatest[Q[1]] != -1 && in_greatest[Q[1]] < Q[2]);
else if (Q[1] == centro)
ans[Q[2]] += (out_greatest[Q[0]] != -1 && out_greatest[Q[0]] < Q[2]);
}
for (auto u : V) {
for (auto id : query_ids[u]) {
array<int,3> Q = query[id];
ans[Q[2]] += (color[Q[1]] != color[Q[0]] && out_smallest[Q[0]] != -1 && in_greatest[Q[1]] != -1
&& in_greatest[Q[1]] < out_smallest[Q[0]] && out_greatest[Q[0]] < Q[2]);
}
if (ttt[u] != -1 && out_greatest[u] != -1 && out_greatest[u] <= ttt[u])
ans[ttt[u]] += os.order_of_key({out_smallest[u], -1});
}
for (auto u : V)
in_greatest[u] = out_smallest[u] = out_greatest[u] = -1;
V = vector<int>();
for (auto u : graph[centro]) {
if (!marked[u.f])
decompose(u.f);
}
}
int main() {
fast;
memset(in_greatest, -1, sizeof(in_greatest));
memset(out_smallest, -1, sizeof(out_smallest));
memset(out_greatest, -1, sizeof(out_greatest));
cin >> N >> K;
graph = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>());
for (int i = 1; i <= N + K - 1; i++) {
char ch;
cin >> ch;
if (ch == 'S') {
int a, b;
cin >> a >> b;
graph[a].push_back({b, i});
graph[b].push_back({a, i});
} else if (ch == 'Q') {
int a, b;
cin >> a >> b;
query_ids[a].push_back(query.size());
query_ids[b].push_back(query.size());
query.push_back({a, b, i});
} else {
int a;
cin >> a;
ttt[a] = i;
query.push_back({a, -1, i});
}
}
for (int i = 1; i <= N; i++) {
sort(graph[i].begin(), graph[i].end(), [&](pair<int,int> A, pair<int,int> B) {
return A.s < B.s;
});
}
decompose(1);
for (auto u : query) {
if (u[1] != -1)
cout << ((ans[u[2]] || u[0] == u[1]) ? "yes\n" : "no\n");
else
cout << ans[u[2]] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
12048 KB |
Output is correct |
2 |
Correct |
42 ms |
13060 KB |
Output is correct |
3 |
Correct |
33 ms |
12808 KB |
Output is correct |
4 |
Correct |
52 ms |
13056 KB |
Output is correct |
5 |
Correct |
65 ms |
13312 KB |
Output is correct |
6 |
Correct |
29 ms |
13060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
12048 KB |
Output is correct |
2 |
Correct |
42 ms |
13060 KB |
Output is correct |
3 |
Correct |
33 ms |
12808 KB |
Output is correct |
4 |
Correct |
52 ms |
13056 KB |
Output is correct |
5 |
Correct |
65 ms |
13312 KB |
Output is correct |
6 |
Correct |
29 ms |
13060 KB |
Output is correct |
7 |
Incorrect |
21 ms |
12044 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12068 KB |
Output is correct |
2 |
Incorrect |
170 ms |
32636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12068 KB |
Output is correct |
2 |
Incorrect |
170 ms |
32636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12044 KB |
Output is correct |
2 |
Incorrect |
284 ms |
32772 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12044 KB |
Output is correct |
2 |
Incorrect |
284 ms |
32772 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
12040 KB |
Output is correct |
2 |
Incorrect |
307 ms |
29444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
12040 KB |
Output is correct |
2 |
Incorrect |
307 ms |
29444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12048 KB |
Output is correct |
2 |
Incorrect |
270 ms |
32776 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12048 KB |
Output is correct |
2 |
Incorrect |
270 ms |
32776 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12036 KB |
Output is correct |
2 |
Correct |
40 ms |
12876 KB |
Output is correct |
3 |
Correct |
33 ms |
12868 KB |
Output is correct |
4 |
Correct |
54 ms |
13056 KB |
Output is correct |
5 |
Correct |
47 ms |
13236 KB |
Output is correct |
6 |
Correct |
30 ms |
13068 KB |
Output is correct |
7 |
Correct |
20 ms |
12048 KB |
Output is correct |
8 |
Incorrect |
147 ms |
32536 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12036 KB |
Output is correct |
2 |
Correct |
40 ms |
12876 KB |
Output is correct |
3 |
Correct |
33 ms |
12868 KB |
Output is correct |
4 |
Correct |
54 ms |
13056 KB |
Output is correct |
5 |
Correct |
47 ms |
13236 KB |
Output is correct |
6 |
Correct |
30 ms |
13068 KB |
Output is correct |
7 |
Correct |
20 ms |
12048 KB |
Output is correct |
8 |
Incorrect |
147 ms |
32536 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |