#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
template <class T>
using ordered_multiset = tree <T, null_type, less_equal <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 2e4 + 5, N2 = 2 * N;
struct query_t{
char type;
int u, v;
query_t(){
}
query_t(char type, int u, int v = 0): type(type), u(u), v(v){
}
friend istream& operator>> (istream& in, query_t& rhs){
in >> rhs.type >> rhs.u;
if (rhs.type != 'C'){
in >> rhs.v;
}
return in;
}
};
int n, q;
query_t a[N2];
vpii adj[N];
int par[N], sz[N];
int ctall;
vi adjct[N];
int parct[N], hct[N];
bool block[N];
void dfs_cpn_ct(int u){
sz[u] = 1;
Fora(&edge, adj[u]){
int v = edge.fi;
if (v == par[u] or block[v]){
continue;
}
par[v] = u;
dfs_cpn_ct(v);
sz[u] += sz[v];
}
}
int dfs_ct(int u){
par[u] = 0;
dfs_cpn_ct(u);
if (sz[u] == 1){
return u;
}
int sz_cpn = sz[u];
while (1){
int v = 0;
Fora(&edge, adj[u]){
int tv = edge.fi;
if (tv == par[u] or block[tv]){
continue;
}
if (sz[tv] * 2 > sz_cpn){
v = tv;
break;
}
}
if (v == 0){
break;
}
u = v;
}
block[u] = 1;
Fora(&edge, adj[u]){
int v = edge.fi;
if (block[v]){
continue;
}
v = dfs_ct(v);
adjct[u].emplace_back(v);
parct[v] = u;
}
return u;
}
void dfs_hct(int u){
Fora(v, adjct[u]){
hct[v] = hct[u] + 1;
dfs_hct(v);
}
}
vi vqueryq[N], vqueryc[N];
int ans[N2];
vi vcpn;
pii pathup[N], pathdown[N];
void dfs_cpn_query(int u){
vcpn.emplace_back(u);
Fora(&edge, adj[u]){
int v = edge.fi, w = edge.se;
if (v == par[u] or block[v]){
continue;
}
par[v] = u;
if (pathup[u].fi == N2){
pathup[v] = make_pair(w, w);
}
else if (w < pathup[u].fi){
pathup[v] = make_pair(w, pathup[u].se);
}
else{
pathup[v] = make_pair(-1, N2 + 1);
}
if (pathdown[u].fi == N2){
pathdown[v] = make_pair(w, w);
}
else if (w > pathdown[u].se){
pathdown[v] = make_pair(pathdown[u].fi, w);
}
else{
pathdown[v] = make_pair(-1, N2 + 1);
}
dfs_cpn_query(v);
}
}
void dfs_query(int ct){
par[ct] = 0;
vcpn.clear();
pathup[ct] = make_pair(N2, 0);
pathdown[ct] = make_pair(N2, 0);
dfs_cpn_query(ct);
Fora(idx, vqueryq[ct]){
int u = a[idx].u, v = a[idx].v;
if (u == ct and v == ct){
ans[idx] = 1;
}
else if (u == ct){
ans[idx] = (pathdown[v].se < idx);
}
else if (v == ct){
ans[idx] = (pathup[u].se < idx);
}
else{
ans[idx] = (pathup[u].se < pathdown[v].fi and pathdown[v].se < idx);
}
}
sort(bend(vqueryc[ct]), [&](int lhs, int rhs){
return pathup[a[lhs].u].se > pathup[a[rhs].u].se;
});
sort(bend(vcpn), [&](int lhs, int rhs){
return pathdown[lhs].fi > pathdown[rhs].fi;
});
ordered_multiset <int> stt;
int ctrvqueryc = 0, ctrvcpn = 0;
while (ctrvqueryc < isz(vqueryc[ct]) or ctrvcpn < isz(vcpn)){
if (ctrvqueryc == isz(vqueryc[ct])){
stt.insert(pathdown[vcpn[ctrvcpn]].se);
ctrvcpn++;
continue;
}
if (ctrvcpn == isz(vcpn)){
ans[vqueryc[ct][ctrvqueryc]] += stt.order_of_key(vqueryc[ct][ctrvqueryc]);
ctrvqueryc++;
continue;
}
if (pathdown[vcpn[ctrvcpn]].fi > pathup[a[vqueryc[ct][ctrvqueryc]].u].se){
stt.insert(pathdown[vcpn[ctrvcpn]].se);
ctrvcpn++;
continue;
}
else{
ans[vqueryc[ct][ctrvqueryc]] += stt.order_of_key(vqueryc[ct][ctrvqueryc]);
ctrvqueryc++;
continue;
}
}
block[ct] = 1;
Fora(vct, adjct[ct]){
dfs_query(vct);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
{
cin >> n >> q;
ForE(i, 1, n + q){
cin >> a[i];
}
}
ForE(i, 1, n + q){
if (a[i].type == 'S'){
adj[a[i].u].emplace_back(a[i].v, i);
adj[a[i].v].emplace_back(a[i].u, i);
}
}
memset(block, 0, sizeof(block));
ctall = dfs_ct(1);
dfs_hct(ctall);
ForE(i, 1, n + q){
if (a[i].type == 'Q'){
swap(a[i].u, a[i].v);
int u = a[i].u, v = a[i].v;
if (hct[u] < hct[v]){
swap(u, v);
}
while (hct[u] > hct[v]){
u = parct[u];
}
while (u != v){
u = parct[u]; v = parct[v];
}
vqueryq[u].emplace_back(i);
}
else if (a[i].type == 'C'){
int u = a[i].u;
while (u != ctall){
vqueryc[u].emplace_back(i);
u = parct[u];
}
vqueryc[u].emplace_back(i);
}
}
memset(block, 0, sizeof(block));
dfs_query(ctall);
ForE(i, 1, n + q){
if (a[i].type == 'Q'){
if (ans[i]){
cout << "yes" << endl;
}
else{
cout << "no" << endl;
}
}
else if (a[i].type == 'C'){
cout << ans[i] << endl;
}
}
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
15184 KB |
Output is correct |
2 |
Correct |
53 ms |
15700 KB |
Output is correct |
3 |
Correct |
53 ms |
15868 KB |
Output is correct |
4 |
Correct |
55 ms |
15864 KB |
Output is correct |
5 |
Correct |
53 ms |
16192 KB |
Output is correct |
6 |
Correct |
39 ms |
15632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
15184 KB |
Output is correct |
2 |
Correct |
53 ms |
15700 KB |
Output is correct |
3 |
Correct |
53 ms |
15868 KB |
Output is correct |
4 |
Correct |
55 ms |
15864 KB |
Output is correct |
5 |
Correct |
53 ms |
16192 KB |
Output is correct |
6 |
Correct |
39 ms |
15632 KB |
Output is correct |
7 |
Incorrect |
46 ms |
15144 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
15180 KB |
Output is correct |
2 |
Correct |
192 ms |
32092 KB |
Output is correct |
3 |
Correct |
164 ms |
32056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
15180 KB |
Output is correct |
2 |
Correct |
192 ms |
32092 KB |
Output is correct |
3 |
Correct |
164 ms |
32056 KB |
Output is correct |
4 |
Incorrect |
34 ms |
15256 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
15108 KB |
Output is correct |
2 |
Correct |
645 ms |
49056 KB |
Output is correct |
3 |
Correct |
597 ms |
49132 KB |
Output is correct |
4 |
Correct |
462 ms |
47872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
15108 KB |
Output is correct |
2 |
Correct |
645 ms |
49056 KB |
Output is correct |
3 |
Correct |
597 ms |
49132 KB |
Output is correct |
4 |
Correct |
462 ms |
47872 KB |
Output is correct |
5 |
Incorrect |
38 ms |
15324 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
15360 KB |
Output is correct |
2 |
Correct |
455 ms |
38340 KB |
Output is correct |
3 |
Correct |
531 ms |
38852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
15360 KB |
Output is correct |
2 |
Correct |
455 ms |
38340 KB |
Output is correct |
3 |
Correct |
531 ms |
38852 KB |
Output is correct |
4 |
Incorrect |
36 ms |
15164 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15164 KB |
Output is correct |
2 |
Correct |
610 ms |
49140 KB |
Output is correct |
3 |
Correct |
614 ms |
49128 KB |
Output is correct |
4 |
Correct |
463 ms |
47904 KB |
Output is correct |
5 |
Correct |
36 ms |
15236 KB |
Output is correct |
6 |
Correct |
452 ms |
38468 KB |
Output is correct |
7 |
Correct |
521 ms |
38876 KB |
Output is correct |
8 |
Correct |
582 ms |
38136 KB |
Output is correct |
9 |
Correct |
540 ms |
38096 KB |
Output is correct |
10 |
Correct |
651 ms |
43904 KB |
Output is correct |
11 |
Correct |
665 ms |
42648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15164 KB |
Output is correct |
2 |
Correct |
610 ms |
49140 KB |
Output is correct |
3 |
Correct |
614 ms |
49128 KB |
Output is correct |
4 |
Correct |
463 ms |
47904 KB |
Output is correct |
5 |
Correct |
36 ms |
15236 KB |
Output is correct |
6 |
Correct |
452 ms |
38468 KB |
Output is correct |
7 |
Correct |
521 ms |
38876 KB |
Output is correct |
8 |
Correct |
582 ms |
38136 KB |
Output is correct |
9 |
Correct |
540 ms |
38096 KB |
Output is correct |
10 |
Correct |
651 ms |
43904 KB |
Output is correct |
11 |
Correct |
665 ms |
42648 KB |
Output is correct |
12 |
Incorrect |
35 ms |
15296 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15288 KB |
Output is correct |
2 |
Correct |
47 ms |
15880 KB |
Output is correct |
3 |
Correct |
47 ms |
15944 KB |
Output is correct |
4 |
Correct |
49 ms |
16040 KB |
Output is correct |
5 |
Correct |
48 ms |
16200 KB |
Output is correct |
6 |
Correct |
47 ms |
15600 KB |
Output is correct |
7 |
Correct |
32 ms |
15248 KB |
Output is correct |
8 |
Correct |
184 ms |
32008 KB |
Output is correct |
9 |
Correct |
178 ms |
32080 KB |
Output is correct |
10 |
Correct |
32 ms |
15176 KB |
Output is correct |
11 |
Correct |
646 ms |
49136 KB |
Output is correct |
12 |
Correct |
624 ms |
49108 KB |
Output is correct |
13 |
Correct |
472 ms |
47916 KB |
Output is correct |
14 |
Correct |
33 ms |
15268 KB |
Output is correct |
15 |
Correct |
464 ms |
38576 KB |
Output is correct |
16 |
Correct |
535 ms |
38848 KB |
Output is correct |
17 |
Correct |
572 ms |
38060 KB |
Output is correct |
18 |
Correct |
536 ms |
38056 KB |
Output is correct |
19 |
Correct |
691 ms |
43956 KB |
Output is correct |
20 |
Correct |
645 ms |
42644 KB |
Output is correct |
21 |
Correct |
201 ms |
34900 KB |
Output is correct |
22 |
Correct |
229 ms |
34124 KB |
Output is correct |
23 |
Correct |
392 ms |
38112 KB |
Output is correct |
24 |
Correct |
388 ms |
37832 KB |
Output is correct |
25 |
Correct |
552 ms |
43424 KB |
Output is correct |
26 |
Correct |
487 ms |
36796 KB |
Output is correct |
27 |
Correct |
457 ms |
37252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15288 KB |
Output is correct |
2 |
Correct |
47 ms |
15880 KB |
Output is correct |
3 |
Correct |
47 ms |
15944 KB |
Output is correct |
4 |
Correct |
49 ms |
16040 KB |
Output is correct |
5 |
Correct |
48 ms |
16200 KB |
Output is correct |
6 |
Correct |
47 ms |
15600 KB |
Output is correct |
7 |
Correct |
32 ms |
15248 KB |
Output is correct |
8 |
Correct |
184 ms |
32008 KB |
Output is correct |
9 |
Correct |
178 ms |
32080 KB |
Output is correct |
10 |
Correct |
32 ms |
15176 KB |
Output is correct |
11 |
Correct |
646 ms |
49136 KB |
Output is correct |
12 |
Correct |
624 ms |
49108 KB |
Output is correct |
13 |
Correct |
472 ms |
47916 KB |
Output is correct |
14 |
Correct |
33 ms |
15268 KB |
Output is correct |
15 |
Correct |
464 ms |
38576 KB |
Output is correct |
16 |
Correct |
535 ms |
38848 KB |
Output is correct |
17 |
Correct |
572 ms |
38060 KB |
Output is correct |
18 |
Correct |
536 ms |
38056 KB |
Output is correct |
19 |
Correct |
691 ms |
43956 KB |
Output is correct |
20 |
Correct |
645 ms |
42644 KB |
Output is correct |
21 |
Correct |
201 ms |
34900 KB |
Output is correct |
22 |
Correct |
229 ms |
34124 KB |
Output is correct |
23 |
Correct |
392 ms |
38112 KB |
Output is correct |
24 |
Correct |
388 ms |
37832 KB |
Output is correct |
25 |
Correct |
552 ms |
43424 KB |
Output is correct |
26 |
Correct |
487 ms |
36796 KB |
Output is correct |
27 |
Correct |
457 ms |
37252 KB |
Output is correct |
28 |
Incorrect |
35 ms |
15280 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |