/**
* author: NimaAryan
* created: 2024-01-22 15:04:53
**/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T, class C = less<>>
using indexed_set = tree<T, null_type, C, rb_tree_tag,
tree_order_statistics_node_update>;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
struct Solver {
int n;
vector<vector<pair<int, int>>> adj;
vector<vector<pair<int, int>>> qry1;
vector<vector<int>> qry2;
vector<int> siz;
vector<bool> alive;
vector<int> join;
vector<int> leave;
vector<int> reach;
vector<int> ans;
Solver(int n, int k) : n(n) {
adj.resize(n);
qry1.resize(n);
qry2.resize(n);
siz.resize(n);
alive.assign(n, true);
join.assign(n, -1);
leave.assign(n, -1);
reach.assign(n, -1);
ans.resize(n - 1 + k);
}
void add_edge(int a, int b, int w) {
adj[a].emplace_back(b, w);
adj[b].emplace_back(a, w);
}
void add_query1(int a, int d, int t) {
qry1[d].emplace_back(a, t);
}
void add_query2(int d, int t) {
qry2[d].push_back(t);
}
void dfs(int v, int p = -1) {
siz[v] = 1;
for (auto [u, _] : adj[v]) {
if (u != p && alive[u]) {
dfs(u, v);
siz[v] += siz[u];
}
}
join[v] = leave[v] = reach[v] = -1;
}
int find_centroid(int v, int z, int p = -1) {
for (auto [u, _] : adj[v]) {
if (u != p && alive[u] && siz[u] > z / 2) {
return find_centroid(u, z, v);
}
}
return v;
}
void solve(int v = 0) {
dfs(v);
int c = find_centroid(v, siz[v]);
{
/* increasing */
auto trav = [&](auto self, int x, int p) -> void {
for (auto [y, z] : adj[x]) {
if (y != p && alive[y] && reach[x] < z) {
leave[y] = leave[x];
reach[y] = z;
self(self, y, x);
}
}
};
for (auto [u, w] : adj[c]) {
if (alive[u]) {
leave[u] = reach[u] = w;
trav(trav, u, c);
}
}
}
{
/* decreasing */
auto trav = [&](auto self, int x, int w, int p) -> void {
for (auto [y, z] : adj[x]) {
if (y != p && alive[y] && w > z) {
join[y] = join[x];
self(self, y, z, x);
}
}
};
for (auto [u, w] : adj[c]) {
if (alive[u]) {
join[u] = w;
trav(trav, u, w, c);
}
}
}
sort(adj[c].begin(), adj[c].end(), [&](auto p, auto q) {
return p.second > q.second;
});
indexed_set<int> can;
vector<int> comp;
auto trav = [&](auto self, int x, int p) -> void {
comp.push_back(x);
for (auto [y, _] : adj[x]) {
if (y != p && alive[y]) {
self(self, y, x);
}
}
};
for (auto [u, _] : adj[c]) {
if (alive[u]) {
comp.clear();
trav(trav, u, c);
for (int d : comp) {
for (auto [a, t] : qry1[d]) {
if (ans[t]) {
continue;
}
if (a == d) {
ans[t] = -1;
continue;
}
if (a == c) {
if (join[d] != -1 && join[d] < t) {
ans[t] = -1;
} else {
ans[t] = -2;
}
continue;
}
if (join[d] != -1 && leave[a] != -1 && join[d] < leave[a] &&
reach[a] != -1 && reach[a] < t) {
ans[t] = -1;
} else {
ans[t] = -2;
}
}
for (int t : qry2[d]) {
if (join[d] != -1 && join[d] < t) {
ans[t] += can.order_of_key(t) + 1;
}
}
}
for (int x : comp) {
if (reach[x] != -1) {
can.insert(reach[x]);
}
}
}
}
{
int d = c;
for (auto [a, t] : qry1[d]) {
if (ans[t]) {
continue;
}
if (a == d) {
ans[t] = -1;
continue;
}
if (reach[a] != -1 && reach[a] < t) {
ans[t] = -1;
} else {
ans[t] = -2;
}
}
for (int t : qry2[d]) {
ans[t] += can.order_of_key(t) + 1;
}
}
alive[c] = false;
for (auto [u, _] : adj[c]) {
if (alive[u]) {
solve(u);
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
Solver t(n, k);
for (int i = 0; i < n - 1 + k; ++i) {
char op;
cin >> op;
if (op == 'S') {
int a, b;
cin >> a >> b;
--a, --b;
t.add_edge(a, b, i);
} else if (op == 'Q') {
int a, d;
cin >> a >> d;
--a, --d;
t.add_query1(a, d, i);
} else {
int d;
cin >> d;
--d;
t.add_query2(d, i);
}
}
t.solve();
for (int i = 0; i < n - 1 + k; ++i) {
int res = t.ans[i];
if (res) {
if (res > 0) {
cout << res << "\n";
} else {
cout << (res == -1 ? "yes" : "no") << "\n";
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3676 KB |
Output is correct |
2 |
Correct |
196 ms |
26564 KB |
Output is correct |
3 |
Correct |
190 ms |
26520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3676 KB |
Output is correct |
2 |
Correct |
196 ms |
26564 KB |
Output is correct |
3 |
Correct |
190 ms |
26520 KB |
Output is correct |
4 |
Correct |
18 ms |
3408 KB |
Output is correct |
5 |
Correct |
189 ms |
26048 KB |
Output is correct |
6 |
Correct |
155 ms |
24264 KB |
Output is correct |
7 |
Correct |
151 ms |
24236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
3664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
3664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |