//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <set>
#include <assert.h>
//#include <ext/pb_ds/assoc_contatiner.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) { cout << a << " ";debug(b...);}
template<class T> void pary (T l, T r) {
while (l != r) cout << (*l) << " ", l++;
cout << endl;
}
#define ll long long
#define maxn 120005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<pii> adj[maxn];
struct query{
int ti, id, to;
query() {ti = 0, id = 0, to = 0;}
query(int x, int y, int z) {ti = x, id = y, to = z;}
};
vector<query> que[maxn];
int qtype[maxn], ans[maxn];
struct BIT{
int bit[maxn];
void modify(int ind, int val) {
for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
}
int query(int ind) {
int ret = 0;
for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
return ret;
}
} bit;
int siz[maxn];
bool mark[maxn];
int found[maxn];
void dfs(int n, int par) {
siz[n] = 1;
for (auto v:adj[n]) {
if (v.ff != par && mark[v.ff]) {
dfs(v.ff, n);
siz[n] += siz[v.ff];
}
}
}
int get_centroid(int n, int par, int tot) {
for (auto v:adj[n]) {
if (v.ff != par && mark[v.ff]) {
if (2 * siz[v.ff] >= tot) return get_centroid(v.ff, n, tot);
}
}
return n;
}
void get_node(int n, int par, int w, vector<pii> & ret, int dir) {
ret.push_back({n, w});
for (auto v:adj[n]) {
if (v.ff != par && mark[v.ff] && (dir == 2 ? true : (dir ? v.ss > w : v.ss < w))) {
get_node(v.ff, n, v.ss, ret, dir);
}
}
}
void recur(int n, int par, vector<int> & ret) {
ret.push_back(n);
for (auto v:adj[n]) {
if (v.ff != par && mark[v.ff]) {
recur(v.ff, n, ret);
}
}
}
void solve(vector<int> a) {
//debug(49);
//pary(a.begin(), a.end());
for (int i:a) mark[i] = 1;
dfs(a[0], 0);
vector<pii> op;
int cent = get_centroid(a[0], 0, a.size());
sort(adj[cent].begin(), adj[cent].end(), [&](pii x, pii y) {return x.ss > y.ss;});
for (auto v:adj[cent]) {
if (!mark[v.ff]) continue;
vector<pii> se;
get_node(v.ff, cent, v.ss, se, 0);
for (auto node:se) {
//debug(7122, node.ff);
for (auto q:que[node.ff]) {
if (q.to == 0) {
ans[q.id] += bit.query(q.ti) + (v.ss <= q.ti ? 1 : 0);
} else if (mark[q.to]) {
if (q.to != cent) ans[q.id] = (found[q.to] && found[q.to] <= q.ti);
else ans[q.id] = v.ss <= q.ti;
}
}
}
se.clear();
get_node(v.ff, cent, v.ss, se, 1);
for (auto node:se) {
//debug(2217, node.ff);
found[node.ff] = node.ss;
bit.modify(node.ss, 1);
}
op.insert(op.end(), se.begin(), se.end());
}
for (auto q:que[cent]) {
if (q.to == 0) {
ans[q.id] += 1 + bit.query(q.ti);
} else if (mark[q.to]){
if (q.to == cent) ans[q.id] = 1;
else ans[q.id] = found[q.to] && found[q.to] <= q.ti;
}
}
for (auto i:op) {
found[i.ff] = 0;
bit.modify(i.ss, -1);
}
vector<vector<int> > rec;
for (auto v:adj[cent]) {
if (!mark[v.ff]) continue;
vector<int> add;
recur(v.ff, cent, add);
rec.push_back(add);
}
for (int i:a) mark[i] = 0;
for (auto i:rec) solve(i);
}
int main() {
io
int n, k;
cin >> n >> k;
int cur = 1, qcnt = 0;
for (int i = 1;i <= n;i++) mark[i] = 1;
for (int i = 0;i < n + k - 1;i++) {
char type;
cin >> type;
if (type == 'S') {
int a, b;
cin >> a >> b;
adj[a].push_back({b, cur});
adj[b].push_back({a, cur});
cur++;
} else if (type == 'Q') {
int a, b;
cin >> a >> b;
qtype[qcnt] = 0;
que[b].push_back(query(cur - 1, qcnt++, a));
} else {
int d;
cin >> d;
qtype[qcnt] = 1;
que[d].push_back(query(cur - 1, qcnt++, 0));
}
}
vector<int> st;
for (int i = 1;i <= n;i++) st.push_back(i);
solve(st);
for (int i = 0;i < k;i++) {
if (qtype[i]) cout << ans[i] << "\n";
else cout << (ans[i] ? "yes" : "no") << "\n";
}
}
/*
6 9
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
C 3
Q 5 1
C 6
Q 1 5
C 1
C 2
C 4
C 5
7 7
S 4 5
S 2 3
S 5 6
S 6 7
S 3 4
S 1 2
C 1
C 2
C 3
C 4
C 5
C 6
C 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
9600 KB |
Output is correct |
2 |
Correct |
48 ms |
9904 KB |
Output is correct |
3 |
Correct |
44 ms |
9772 KB |
Output is correct |
4 |
Correct |
49 ms |
10048 KB |
Output is correct |
5 |
Correct |
49 ms |
10312 KB |
Output is correct |
6 |
Correct |
46 ms |
10028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
9600 KB |
Output is correct |
2 |
Correct |
48 ms |
9904 KB |
Output is correct |
3 |
Correct |
44 ms |
9772 KB |
Output is correct |
4 |
Correct |
49 ms |
10048 KB |
Output is correct |
5 |
Correct |
49 ms |
10312 KB |
Output is correct |
6 |
Correct |
46 ms |
10028 KB |
Output is correct |
7 |
Correct |
35 ms |
9360 KB |
Output is correct |
8 |
Correct |
52 ms |
9920 KB |
Output is correct |
9 |
Correct |
43 ms |
9604 KB |
Output is correct |
10 |
Correct |
50 ms |
9976 KB |
Output is correct |
11 |
Correct |
49 ms |
10252 KB |
Output is correct |
12 |
Correct |
43 ms |
9692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
9636 KB |
Output is correct |
2 |
Correct |
202 ms |
24084 KB |
Output is correct |
3 |
Correct |
202 ms |
24116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
9636 KB |
Output is correct |
2 |
Correct |
202 ms |
24084 KB |
Output is correct |
3 |
Correct |
202 ms |
24116 KB |
Output is correct |
4 |
Correct |
34 ms |
9424 KB |
Output is correct |
5 |
Correct |
212 ms |
26572 KB |
Output is correct |
6 |
Correct |
146 ms |
24592 KB |
Output is correct |
7 |
Correct |
154 ms |
24892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9588 KB |
Output is correct |
2 |
Correct |
387 ms |
26968 KB |
Output is correct |
3 |
Correct |
375 ms |
26972 KB |
Output is correct |
4 |
Correct |
313 ms |
28296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9588 KB |
Output is correct |
2 |
Correct |
387 ms |
26968 KB |
Output is correct |
3 |
Correct |
375 ms |
26972 KB |
Output is correct |
4 |
Correct |
313 ms |
28296 KB |
Output is correct |
5 |
Correct |
34 ms |
9384 KB |
Output is correct |
6 |
Correct |
390 ms |
29352 KB |
Output is correct |
7 |
Correct |
340 ms |
30404 KB |
Output is correct |
8 |
Correct |
395 ms |
29828 KB |
Output is correct |
9 |
Correct |
391 ms |
29652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9540 KB |
Output is correct |
2 |
Correct |
299 ms |
19508 KB |
Output is correct |
3 |
Correct |
309 ms |
18284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9540 KB |
Output is correct |
2 |
Correct |
299 ms |
19508 KB |
Output is correct |
3 |
Correct |
309 ms |
18284 KB |
Output is correct |
4 |
Correct |
34 ms |
9328 KB |
Output is correct |
5 |
Correct |
322 ms |
22452 KB |
Output is correct |
6 |
Correct |
314 ms |
21328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9540 KB |
Output is correct |
2 |
Correct |
405 ms |
26992 KB |
Output is correct |
3 |
Correct |
376 ms |
26980 KB |
Output is correct |
4 |
Correct |
326 ms |
28264 KB |
Output is correct |
5 |
Correct |
35 ms |
9616 KB |
Output is correct |
6 |
Correct |
307 ms |
19568 KB |
Output is correct |
7 |
Correct |
304 ms |
18312 KB |
Output is correct |
8 |
Correct |
320 ms |
19272 KB |
Output is correct |
9 |
Correct |
358 ms |
19000 KB |
Output is correct |
10 |
Correct |
472 ms |
23452 KB |
Output is correct |
11 |
Correct |
406 ms |
22212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9540 KB |
Output is correct |
2 |
Correct |
405 ms |
26992 KB |
Output is correct |
3 |
Correct |
376 ms |
26980 KB |
Output is correct |
4 |
Correct |
326 ms |
28264 KB |
Output is correct |
5 |
Correct |
35 ms |
9616 KB |
Output is correct |
6 |
Correct |
307 ms |
19568 KB |
Output is correct |
7 |
Correct |
304 ms |
18312 KB |
Output is correct |
8 |
Correct |
320 ms |
19272 KB |
Output is correct |
9 |
Correct |
358 ms |
19000 KB |
Output is correct |
10 |
Correct |
472 ms |
23452 KB |
Output is correct |
11 |
Correct |
406 ms |
22212 KB |
Output is correct |
12 |
Correct |
33 ms |
9496 KB |
Output is correct |
13 |
Correct |
395 ms |
29296 KB |
Output is correct |
14 |
Correct |
342 ms |
30400 KB |
Output is correct |
15 |
Correct |
385 ms |
29760 KB |
Output is correct |
16 |
Correct |
376 ms |
29632 KB |
Output is correct |
17 |
Correct |
36 ms |
10288 KB |
Output is correct |
18 |
Correct |
322 ms |
22504 KB |
Output is correct |
19 |
Correct |
315 ms |
21444 KB |
Output is correct |
20 |
Correct |
334 ms |
22408 KB |
Output is correct |
21 |
Correct |
393 ms |
22356 KB |
Output is correct |
22 |
Correct |
487 ms |
25532 KB |
Output is correct |
23 |
Correct |
571 ms |
26468 KB |
Output is correct |
24 |
Correct |
520 ms |
26396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
9740 KB |
Output is correct |
2 |
Correct |
49 ms |
9924 KB |
Output is correct |
3 |
Correct |
44 ms |
9816 KB |
Output is correct |
4 |
Correct |
50 ms |
10052 KB |
Output is correct |
5 |
Correct |
48 ms |
10308 KB |
Output is correct |
6 |
Correct |
46 ms |
10024 KB |
Output is correct |
7 |
Correct |
34 ms |
9700 KB |
Output is correct |
8 |
Correct |
218 ms |
24032 KB |
Output is correct |
9 |
Correct |
205 ms |
24100 KB |
Output is correct |
10 |
Correct |
35 ms |
9520 KB |
Output is correct |
11 |
Correct |
388 ms |
26984 KB |
Output is correct |
12 |
Correct |
411 ms |
26940 KB |
Output is correct |
13 |
Correct |
334 ms |
28236 KB |
Output is correct |
14 |
Correct |
34 ms |
9548 KB |
Output is correct |
15 |
Correct |
341 ms |
19496 KB |
Output is correct |
16 |
Correct |
320 ms |
18336 KB |
Output is correct |
17 |
Correct |
321 ms |
19136 KB |
Output is correct |
18 |
Correct |
357 ms |
18952 KB |
Output is correct |
19 |
Correct |
462 ms |
23444 KB |
Output is correct |
20 |
Correct |
415 ms |
22144 KB |
Output is correct |
21 |
Correct |
229 ms |
25112 KB |
Output is correct |
22 |
Correct |
245 ms |
23160 KB |
Output is correct |
23 |
Correct |
278 ms |
19104 KB |
Output is correct |
24 |
Correct |
273 ms |
19044 KB |
Output is correct |
25 |
Correct |
417 ms |
26176 KB |
Output is correct |
26 |
Correct |
348 ms |
17500 KB |
Output is correct |
27 |
Correct |
330 ms |
17780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
9740 KB |
Output is correct |
2 |
Correct |
49 ms |
9924 KB |
Output is correct |
3 |
Correct |
44 ms |
9816 KB |
Output is correct |
4 |
Correct |
50 ms |
10052 KB |
Output is correct |
5 |
Correct |
48 ms |
10308 KB |
Output is correct |
6 |
Correct |
46 ms |
10024 KB |
Output is correct |
7 |
Correct |
34 ms |
9700 KB |
Output is correct |
8 |
Correct |
218 ms |
24032 KB |
Output is correct |
9 |
Correct |
205 ms |
24100 KB |
Output is correct |
10 |
Correct |
35 ms |
9520 KB |
Output is correct |
11 |
Correct |
388 ms |
26984 KB |
Output is correct |
12 |
Correct |
411 ms |
26940 KB |
Output is correct |
13 |
Correct |
334 ms |
28236 KB |
Output is correct |
14 |
Correct |
34 ms |
9548 KB |
Output is correct |
15 |
Correct |
341 ms |
19496 KB |
Output is correct |
16 |
Correct |
320 ms |
18336 KB |
Output is correct |
17 |
Correct |
321 ms |
19136 KB |
Output is correct |
18 |
Correct |
357 ms |
18952 KB |
Output is correct |
19 |
Correct |
462 ms |
23444 KB |
Output is correct |
20 |
Correct |
415 ms |
22144 KB |
Output is correct |
21 |
Correct |
229 ms |
25112 KB |
Output is correct |
22 |
Correct |
245 ms |
23160 KB |
Output is correct |
23 |
Correct |
278 ms |
19104 KB |
Output is correct |
24 |
Correct |
273 ms |
19044 KB |
Output is correct |
25 |
Correct |
417 ms |
26176 KB |
Output is correct |
26 |
Correct |
348 ms |
17500 KB |
Output is correct |
27 |
Correct |
330 ms |
17780 KB |
Output is correct |
28 |
Correct |
34 ms |
9416 KB |
Output is correct |
29 |
Correct |
51 ms |
11028 KB |
Output is correct |
30 |
Correct |
43 ms |
10692 KB |
Output is correct |
31 |
Correct |
51 ms |
11076 KB |
Output is correct |
32 |
Correct |
50 ms |
11296 KB |
Output is correct |
33 |
Correct |
44 ms |
10820 KB |
Output is correct |
34 |
Correct |
34 ms |
10308 KB |
Output is correct |
35 |
Correct |
225 ms |
26700 KB |
Output is correct |
36 |
Correct |
141 ms |
24620 KB |
Output is correct |
37 |
Correct |
145 ms |
24864 KB |
Output is correct |
38 |
Correct |
35 ms |
10180 KB |
Output is correct |
39 |
Correct |
390 ms |
30044 KB |
Output is correct |
40 |
Correct |
342 ms |
31172 KB |
Output is correct |
41 |
Correct |
394 ms |
29704 KB |
Output is correct |
42 |
Correct |
381 ms |
29628 KB |
Output is correct |
43 |
Correct |
36 ms |
10180 KB |
Output is correct |
44 |
Correct |
333 ms |
22528 KB |
Output is correct |
45 |
Correct |
308 ms |
21356 KB |
Output is correct |
46 |
Correct |
342 ms |
22540 KB |
Output is correct |
47 |
Correct |
370 ms |
22404 KB |
Output is correct |
48 |
Correct |
484 ms |
25752 KB |
Output is correct |
49 |
Correct |
598 ms |
26400 KB |
Output is correct |
50 |
Correct |
515 ms |
26476 KB |
Output is correct |
51 |
Correct |
178 ms |
27100 KB |
Output is correct |
52 |
Correct |
153 ms |
25656 KB |
Output is correct |
53 |
Correct |
150 ms |
25400 KB |
Output is correct |
54 |
Correct |
154 ms |
25760 KB |
Output is correct |
55 |
Correct |
162 ms |
26224 KB |
Output is correct |
56 |
Correct |
251 ms |
21676 KB |
Output is correct |
57 |
Correct |
318 ms |
27852 KB |
Output is correct |
58 |
Correct |
419 ms |
21104 KB |
Output is correct |
59 |
Correct |
344 ms |
21220 KB |
Output is correct |