#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n) - 1; i >= 0; --i)
#define rrep2(i, n, t) for(ll i = ll(n) - 1; i >= ll(t); --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n >> q;
vvp G(n);
vp es;
vector qs(n, vector<tuple<int, int, int>>());
rep(_, n + q - 1) {
char c;
cin >> c;
if (c == 'S') {
int a, b;
cin >> a >> b;
--a, --b;
G[a].eb(b, SZ(es));
G[b].eb(a, SZ(es));
es.eb(a, b);
} else if (c == 'Q') {
int a, d;
cin >> a >> d;
--a, --d;
qs[SZ(es)].eb(0, d, a);
} else {
int d;
cin >> d;
--d;
qs[SZ(es)].eb(1, d, 0);
}
}
vi dep(n);
vvi par(20, vi(n));
vi pid(n), inc(n), dec(n);
auto dfs = [&](auto &dfs, int u, int p, int d, int _pid) -> void {
dep[u] = d;
par[0][u] = p;
pid[u] = _pid;
for (auto[v, id]: G[u]) {
if (v == p) continue;
inc[v] = (u and _pid > id ? inc[u] : 0) + 1;
dec[v] = (u and _pid < id ? dec[u] : 0) + 1;
dfs(dfs, v, u, d + 1, id);
}
};
dfs(dfs, 0, -1, 0, -1);
auto la = [&](int u, int d) {
if (!d) return u;
rep(k, 20) {
if (d >> k & 1) {
u = par[k][u];
assert(u != -1);
}
}
return u;
};
auto lca = [&](int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
v = la(v, dep[v] - dep[u]);
if (u == v) return u;
rrep(k, 20) {
if (par[k][u] != par[k][v]) {
u = par[k][u];
v = par[k][v];
}
}
return par[0][u];
};
vs ans(q);
int it = 0;
rep(i, n) {
for (auto[t, d, a]: qs[i]) {
if (t == 0) {
int l = lca(d, a);
int hd = dep[d] - dep[l];
int ha = dep[a] - dep[l];
int when;
if (d == a) {
when = -1;
} else if (l == d) {
when = (dec[a] >= ha ? pid[a] : inf);
} else if (l == a) {
when = (inc[d] >= hd ? pid[la(d, hd - 1)] : inf);
} else {
// d -> (edge : x) -> l -> (edge : y) -> a
int x = pid[la(d, hd - 1)];
int y = pid[la(a, ha - 1)];
when = (inc[d] >= hd and dec[a] >= ha and x < y ? pid[a] : inf);
}
ans[it] = (when < i ? "yes" : "no");
} else {
}
++it;
}
}
rep(i, q) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7040 KB |
Output is correct |
2 |
Correct |
156 ms |
33560 KB |
Output is correct |
3 |
Correct |
125 ms |
33608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7040 KB |
Output is correct |
2 |
Correct |
156 ms |
33560 KB |
Output is correct |
3 |
Correct |
125 ms |
33608 KB |
Output is correct |
4 |
Incorrect |
28 ms |
6928 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
7032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
7032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |