#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 120000, LGN = 18;
vector<pair<unsigned, unsigned>> g[N], q1[N];
vector<unsigned> q2[N];
unsigned subtree_size[N], min_time[N], max_time[N], subtree[N];
vector<pair<unsigned, string>> ans;
uint8_t monotonicity[N];
bitset<N> removed;
unsigned find_centroid(unsigned u, unsigned p, unsigned n)
{
subtree_size[u] = 1;
for (auto const &[v, t] : g[u])
if (!removed[v] && v != p)
{
unsigned x = find_centroid(v, u, n);
if (x != UINT_MAX)
return x;
subtree_size[u] += subtree_size[v];
}
return subtree_size[u] > n / 2 ? u : UINT_MAX;
}
void trav(unsigned u, unsigned p, unsigned t0, unsigned min_t, unsigned max_t, uint8_t m, unsigned s)
{
subtree_size[u] = 1;
monotonicity[u] = m;
min_time[u] = min_t;
max_time[u] = max_t;
subtree[u] = s;
for (auto const &[v, t] : g[u])
if (!removed[v] && v != p)
{
trav(v, u, t, min(min_t, t), max(max_t, t), m & (t < t0 ? 1 : 2), s);
subtree_size[u] += subtree_size[v];
}
}
void answer_queries(unsigned u, unsigned p)
{
for (size_t i = 0; i < q1[u].size(); ++i)
{
auto const [v, t] = q1[u][i];
if (subtree[v] != subtree[u])
{
ans.emplace_back(t, ((monotonicity[u] & 2) && (monotonicity[v] & 1) &&
max_time[v] < min_time[u] && t > max_time[u])
? "yes"
: "no");
swap(q1[u][i--], q1[u].back());
q1[u].pop_back();
}
}
for (auto const &[v, t] : g[u])
if (!removed[v] && v != p)
answer_queries(v, u);
}
void decompose(unsigned u, unsigned n)
{
unsigned const c = find_centroid(u, UINT_MAX, n);
removed[c] = 1;
monotonicity[c] = 3;
max_time[c] = 0;
min_time[c] = UINT_MAX;
subtree[c] = UINT_MAX;
for (auto const &[v, t] : g[c])
if (!removed[v])
trav(v, c, t, t, t, 3, v);
answer_queries(c, UINT_MAX);
for (auto const &[v, t] : g[c])
if (!removed[v])
decompose(v, subtree_size[v]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, k;
cin >> n >> k;
for (size_t i = 0; i < n + k - 1; ++i)
{
char c;
cin >> c;
if (c == 'S')
{
unsigned u, v;
cin >> u >> v;
g[u - 1].emplace_back(v - 1, i + 1);
g[v - 1].emplace_back(u - 1, i + 1);
}
else if (c == 'Q')
{
unsigned u, v;
cin >> u >> v;
if (u == v)
ans.emplace_back(i + 1, "yes");
else
q1[u - 1].emplace_back(v - 1, i + 1);
}
else
{
unsigned d;
cin >> d;
q2[d - 1].push_back(i + 1);
ans.emplace_back(i + 1, "-1");
}
}
decompose(0, n);
sort(ans.begin(), ans.end());
for (auto const &[t, a] : ans)
cout << a << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
16400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |