#include <bits/stdc++.h>
using namespace std;
struct Query {
int chunk_id;
int server_id;
int time;
enum Type {
query = 'Q', count = 'C'
} type;
};
struct Node {
vector<const Node*> children;
const Node *parent;
int depth;
int time;
};
vector<Node> dfs(int u, int size, const vector<vector<pair<int, int>>> &graph) {
vector<Node> nodes(size + 1);
vector<int> stack;
stack.reserve(size);
stack.push_back(u);
nodes[u].depth = 1;
while (!stack.empty()) {
int u = stack.back();
stack.pop_back();
for (const auto &edge : graph[u]) {
int v = edge.first;
int t = edge.second;
if (nodes[v].depth == 0) {
nodes[v].depth = nodes[u].depth + 1;
nodes[v].parent = &nodes[u];
nodes[v].time = t;
stack.push_back(v);
}
}
}
return nodes;
}
int main() {
int n, k;
cin >> n >> k;
vector<Query> queries;
queries.reserve(k);
vector<vector<pair<int, int>>> graph(n + 1);
for (int i = 0; i < n + k - 1; i++) {
char query_type;
cin >> query_type;
switch (query_type) {
case 'S':
int u, v;
cin >> u >> v;
graph[u].emplace_back(v, i);
graph[v].emplace_back(u, i);
break;
case 'Q':
case 'C':
int chunk_id, server_id;
if (query_type == 'Q')
cin >> server_id;
cin >> chunk_id;
queries.emplace_back(chunk_id, server_id, i, static_cast<Query::Type>(query_type));
break;
default:
__builtin_unreachable();
}
}
vector<Node> nodes = dfs(1, n, graph);
graph.clear();
graph.shrink_to_fit();
for (const auto &query : queries) {
int chunk_id = query.chunk_id;
int server_id = query.server_id;
int time = query.time;
auto query_type = query.type;
assert(query_type == Query::query);
const Node *u = &nodes[chunk_id], *v = &nodes[server_id];
bool result = time > v->time;
while (u->depth > v->depth) {
int time = u->time;
u = u->parent;
if (u->time < time) {
result = false;
break;
}
}
while (v->depth > u->depth) {
int time = v->time;
v = v->parent;
if (v->time > time) {
result = false;
break;
}
}
while (result && u != v) {
int time = u->time;
u = u->parent;
if (u->time < time) {
result = false;
break;
}
time = v->time;
v = v->parent;
if (v->time > time) {
result = false;
break;
}
}
cout << (result ? "yes" : "no") << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |