#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 120001;
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();
--i;
}
}
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 = 1; i <= n + k - 1; ++i)
{
char c;
cin >> c;
if (c == 'S')
{
unsigned u, v;
cin >> u >> v;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
else if (c == 'Q')
{
unsigned u, v;
cin >> u >> v;
if (u == v)
ans.emplace_back(i, "yes");
else
q1[u].emplace_back(v, i);
}
else
{
unsigned d;
cin >> d;
q2[d].push_back(i);
}
}
decompose(1, n);
sort(ans.begin(), ans.end());
for (auto const &[t, a] : ans)
cout << a << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
15508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
15508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
15500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
15500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
15552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
15552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
15540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
15540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
15508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
15508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
15588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
15588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |