#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>>());
bool uni = true;
bool line = true;
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);
uni &= (!a or !b);
line &= abs(a - b) == 1;
} 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);
rep(k, 19) {
rep(i, n) {
if (par[k][i] == -1) par[k + 1][i] = -1;
else par[k + 1][i] = par[k][par[k][i]];
}
}
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;
vi ord(n);
iota(all(ord),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 {
int val = 0;
if (uni) {
if (!d) {
val = i + 1;
} else {
val = max<int>(1, i - pid[d] + 1);
}
} else if (line) {
val = 1;
val += d + 1 - (upper_bound(ord.begin() + d - inc[d] + 1, ord.begin() + d + 1, i,
[&](int key, int now) { return pid[now] < key; }) - ord.begin());
int _d = d;
val += (lower_bound(ord.begin() + d + 1, ord.end(), i,
[&](int now, int key) {
return pid[now] < key and dec[now] >= now - _d;
}) - ord.begin()) - d - 1;
}
ans[it] = to_string(val);
}
++it;
}
}
if (n <= 4000) {
vector<bitset<4000>> bt(n);
rep(i, n) bt[i].set(i);
vi cnt(n, 1);
it = 0;
rep(i, n) {
for (auto[t, d, a]: qs[i]) {
if (t == 1) {
ans[it] = to_string(cnt[d]);
}
++it;
}
if (i < n - 1) {
auto[u, v] = es[i];
rep(j, n) if (bt[u][j] or bt[v][j]) ++cnt[j];
bt[u] |= bt[v];
bt[v] = bt[u];
}
}
}
rep(i, q) cout << ans[i] << '\n';
}
//4 4
//S 1 2
//S 1 3
//S 3 4
//Q 2 1
//Q 2 2
//Q 2 3
//Q 2 4
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6660 KB |
Output is correct |
2 |
Correct |
75 ms |
9584 KB |
Output is correct |
3 |
Correct |
73 ms |
9804 KB |
Output is correct |
4 |
Correct |
77 ms |
9720 KB |
Output is correct |
5 |
Correct |
58 ms |
9164 KB |
Output is correct |
6 |
Correct |
85 ms |
9368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6660 KB |
Output is correct |
2 |
Correct |
75 ms |
9584 KB |
Output is correct |
3 |
Correct |
73 ms |
9804 KB |
Output is correct |
4 |
Correct |
77 ms |
9720 KB |
Output is correct |
5 |
Correct |
58 ms |
9164 KB |
Output is correct |
6 |
Correct |
85 ms |
9368 KB |
Output is correct |
7 |
Correct |
46 ms |
6664 KB |
Output is correct |
8 |
Correct |
70 ms |
9424 KB |
Output is correct |
9 |
Correct |
66 ms |
9720 KB |
Output is correct |
10 |
Correct |
71 ms |
9420 KB |
Output is correct |
11 |
Correct |
64 ms |
9092 KB |
Output is correct |
12 |
Correct |
104 ms |
9232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
6716 KB |
Output is correct |
2 |
Correct |
129 ms |
32416 KB |
Output is correct |
3 |
Correct |
154 ms |
32396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
6716 KB |
Output is correct |
2 |
Correct |
129 ms |
32416 KB |
Output is correct |
3 |
Correct |
154 ms |
32396 KB |
Output is correct |
4 |
Correct |
30 ms |
6736 KB |
Output is correct |
5 |
Correct |
133 ms |
31684 KB |
Output is correct |
6 |
Correct |
90 ms |
31904 KB |
Output is correct |
7 |
Correct |
93 ms |
31792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
6648 KB |
Output is correct |
2 |
Correct |
152 ms |
35816 KB |
Output is correct |
3 |
Correct |
133 ms |
35776 KB |
Output is correct |
4 |
Correct |
114 ms |
35756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
6648 KB |
Output is correct |
2 |
Correct |
152 ms |
35816 KB |
Output is correct |
3 |
Correct |
133 ms |
35776 KB |
Output is correct |
4 |
Correct |
114 ms |
35756 KB |
Output is correct |
5 |
Correct |
31 ms |
6668 KB |
Output is correct |
6 |
Correct |
148 ms |
35500 KB |
Output is correct |
7 |
Correct |
117 ms |
37976 KB |
Output is correct |
8 |
Correct |
146 ms |
37460 KB |
Output is correct |
9 |
Correct |
127 ms |
37388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6716 KB |
Output is correct |
2 |
Correct |
137 ms |
30844 KB |
Output is correct |
3 |
Correct |
138 ms |
30080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6716 KB |
Output is correct |
2 |
Correct |
137 ms |
30844 KB |
Output is correct |
3 |
Correct |
138 ms |
30080 KB |
Output is correct |
4 |
Correct |
36 ms |
6716 KB |
Output is correct |
5 |
Incorrect |
112 ms |
30584 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6712 KB |
Output is correct |
2 |
Correct |
156 ms |
35776 KB |
Output is correct |
3 |
Correct |
127 ms |
35776 KB |
Output is correct |
4 |
Correct |
116 ms |
35780 KB |
Output is correct |
5 |
Correct |
53 ms |
6716 KB |
Output is correct |
6 |
Correct |
136 ms |
30836 KB |
Output is correct |
7 |
Correct |
136 ms |
30116 KB |
Output is correct |
8 |
Correct |
179 ms |
30716 KB |
Output is correct |
9 |
Correct |
161 ms |
32828 KB |
Output is correct |
10 |
Correct |
139 ms |
33720 KB |
Output is correct |
11 |
Correct |
201 ms |
33080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6712 KB |
Output is correct |
2 |
Correct |
156 ms |
35776 KB |
Output is correct |
3 |
Correct |
127 ms |
35776 KB |
Output is correct |
4 |
Correct |
116 ms |
35780 KB |
Output is correct |
5 |
Correct |
53 ms |
6716 KB |
Output is correct |
6 |
Correct |
136 ms |
30836 KB |
Output is correct |
7 |
Correct |
136 ms |
30116 KB |
Output is correct |
8 |
Correct |
179 ms |
30716 KB |
Output is correct |
9 |
Correct |
161 ms |
32828 KB |
Output is correct |
10 |
Correct |
139 ms |
33720 KB |
Output is correct |
11 |
Correct |
201 ms |
33080 KB |
Output is correct |
12 |
Correct |
42 ms |
6732 KB |
Output is correct |
13 |
Correct |
134 ms |
35508 KB |
Output is correct |
14 |
Correct |
116 ms |
38068 KB |
Output is correct |
15 |
Correct |
136 ms |
37440 KB |
Output is correct |
16 |
Correct |
123 ms |
37492 KB |
Output is correct |
17 |
Correct |
38 ms |
6956 KB |
Output is correct |
18 |
Incorrect |
103 ms |
32856 KB |
Extra information in the output file |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6692 KB |
Output is correct |
2 |
Correct |
80 ms |
9588 KB |
Output is correct |
3 |
Correct |
68 ms |
9920 KB |
Output is correct |
4 |
Correct |
76 ms |
9656 KB |
Output is correct |
5 |
Correct |
62 ms |
9188 KB |
Output is correct |
6 |
Correct |
92 ms |
9368 KB |
Output is correct |
7 |
Correct |
37 ms |
6716 KB |
Output is correct |
8 |
Correct |
135 ms |
32016 KB |
Output is correct |
9 |
Correct |
172 ms |
31948 KB |
Output is correct |
10 |
Correct |
32 ms |
6664 KB |
Output is correct |
11 |
Correct |
133 ms |
35700 KB |
Output is correct |
12 |
Correct |
144 ms |
35804 KB |
Output is correct |
13 |
Correct |
115 ms |
35756 KB |
Output is correct |
14 |
Correct |
37 ms |
6696 KB |
Output is correct |
15 |
Correct |
131 ms |
30828 KB |
Output is correct |
16 |
Correct |
138 ms |
30252 KB |
Output is correct |
17 |
Correct |
194 ms |
30656 KB |
Output is correct |
18 |
Correct |
151 ms |
32836 KB |
Output is correct |
19 |
Correct |
151 ms |
33756 KB |
Output is correct |
20 |
Correct |
229 ms |
33080 KB |
Output is correct |
21 |
Correct |
143 ms |
32012 KB |
Output is correct |
22 |
Correct |
149 ms |
31964 KB |
Output is correct |
23 |
Correct |
149 ms |
32480 KB |
Output is correct |
24 |
Correct |
146 ms |
32500 KB |
Output is correct |
25 |
Correct |
165 ms |
32872 KB |
Output is correct |
26 |
Correct |
116 ms |
30532 KB |
Output is correct |
27 |
Correct |
116 ms |
29888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6692 KB |
Output is correct |
2 |
Correct |
80 ms |
9588 KB |
Output is correct |
3 |
Correct |
68 ms |
9920 KB |
Output is correct |
4 |
Correct |
76 ms |
9656 KB |
Output is correct |
5 |
Correct |
62 ms |
9188 KB |
Output is correct |
6 |
Correct |
92 ms |
9368 KB |
Output is correct |
7 |
Correct |
37 ms |
6716 KB |
Output is correct |
8 |
Correct |
135 ms |
32016 KB |
Output is correct |
9 |
Correct |
172 ms |
31948 KB |
Output is correct |
10 |
Correct |
32 ms |
6664 KB |
Output is correct |
11 |
Correct |
133 ms |
35700 KB |
Output is correct |
12 |
Correct |
144 ms |
35804 KB |
Output is correct |
13 |
Correct |
115 ms |
35756 KB |
Output is correct |
14 |
Correct |
37 ms |
6696 KB |
Output is correct |
15 |
Correct |
131 ms |
30828 KB |
Output is correct |
16 |
Correct |
138 ms |
30252 KB |
Output is correct |
17 |
Correct |
194 ms |
30656 KB |
Output is correct |
18 |
Correct |
151 ms |
32836 KB |
Output is correct |
19 |
Correct |
151 ms |
33756 KB |
Output is correct |
20 |
Correct |
229 ms |
33080 KB |
Output is correct |
21 |
Correct |
143 ms |
32012 KB |
Output is correct |
22 |
Correct |
149 ms |
31964 KB |
Output is correct |
23 |
Correct |
149 ms |
32480 KB |
Output is correct |
24 |
Correct |
146 ms |
32500 KB |
Output is correct |
25 |
Correct |
165 ms |
32872 KB |
Output is correct |
26 |
Correct |
116 ms |
30532 KB |
Output is correct |
27 |
Correct |
116 ms |
29888 KB |
Output is correct |
28 |
Correct |
38 ms |
6664 KB |
Output is correct |
29 |
Correct |
76 ms |
9468 KB |
Output is correct |
30 |
Correct |
66 ms |
9720 KB |
Output is correct |
31 |
Correct |
65 ms |
9544 KB |
Output is correct |
32 |
Correct |
65 ms |
9012 KB |
Output is correct |
33 |
Correct |
80 ms |
9168 KB |
Output is correct |
34 |
Correct |
28 ms |
6744 KB |
Output is correct |
35 |
Correct |
119 ms |
31896 KB |
Output is correct |
36 |
Correct |
94 ms |
32200 KB |
Output is correct |
37 |
Correct |
85 ms |
32084 KB |
Output is correct |
38 |
Correct |
35 ms |
6752 KB |
Output is correct |
39 |
Correct |
133 ms |
35548 KB |
Output is correct |
40 |
Correct |
112 ms |
38008 KB |
Output is correct |
41 |
Correct |
135 ms |
37416 KB |
Output is correct |
42 |
Correct |
128 ms |
37492 KB |
Output is correct |
43 |
Correct |
35 ms |
7008 KB |
Output is correct |
44 |
Incorrect |
103 ms |
32860 KB |
Extra information in the output file |
45 |
Halted |
0 ms |
0 KB |
- |