#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 3e5 + 9;
const ll inf = 1e9 + 7;
vector<ll> qcnt[N];
vector<LL> g[N],qif[N];;
ll n,Q,ans[N],ban[N],bit[N],child[N];
void upd(ll p,ll val){
for (ll i = p;i < N;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
void dfs_sz(ll u,ll p){
child[u] = 1;
for (auto i : g[u]){
ll v = i.sc;
if (v == p || ban[v]) continue;
dfs_sz(v,u); child[u] += child[v];
}
}
ll Find_cent(ll u,ll p,ll sz){
ll mx = 0;
for (auto i : g[u])
if (i.sc != p && !ban[i.sc] && child[mx] < child[i.sc]) mx = i.sc;
if (max(child[mx],sz - child[u])*2 <= sz) return u;
return Find_cent(mx,u,sz);
}
ll in_time[N];
void dfs_in(ll u,ll p,ll lastVal){
in_time[u] = lastVal; upd(lastVal,1);
for (auto i : g[u]) if (i.sc != p && !ban[i.sc]){
if (i.fs >= lastVal) dfs_in(i.sc,u,i.fs);
}
}
void dfs_out(ll u,ll p,ll lastVal){
in_time[u] = inf; upd(lastVal,-1);
for (auto i : g[u]) if (i.sc != p && !ban[i.sc]){
if (i.fs >= lastVal) dfs_out(i.sc,u,i.fs);
}
}
void Answer(ll now){
for (auto i : qcnt[now]) ans[i] += Get(i);
for (auto i : qif[now])
if (in_time[i.sc] <= i.fs) ans[i.fs] = -1;
}
void dfs_solve(ll u,ll p,ll lastVal){
Answer(u);
for (auto i : g[u]) if (i.sc != p && !ban[i.sc]){
if (i.fs <= lastVal) dfs_solve(i.sc,u,i.fs);
}
}
void solve(ll now,ll par){
dfs_sz(now,0); ll cent = Find_cent(now,0,child[now]);
sort(g[cent].begin(),g[cent].end());
dfs_in(cent,0,1);
Answer(cent);
for (auto i : g[cent]) if (i.sc != par && !ban[i.sc]){
dfs_out(i.sc,cent,i.fs);
upd(in_time[cent],-1); in_time[cent] = i.fs;
upd(in_time[cent],1);
dfs_solve(i.sc,cent,i.fs);
}
ban[cent] = 1; upd(in_time[cent],-1); in_time[cent] = inf;
for (auto i : g[cent])
if (i.sc != par && !ban[i.sc]) solve(i.sc,cent);
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>n>>Q; Q += n - 1;
for (ll i = 1;i <= Q;i++){
char c; cin>>c; ll x,y;
if (c == 'S'){
ans[i] = -69;
cin>>x>>y;
g[x].push_back({i,y}); g[y].push_back({i,x});
}
else if (c == 'C'){
cin>>x; ans[i] = 0;
qcnt[x].push_back(i);
}
else{
cin>>x>>y; ans[i] = -2;
qif[y].push_back({i,x});
}
}
//return 0;
fill(in_time,in_time + n + 1,inf); //ban[2] = 1; solve(3,0);
solve(1,0);
for (ll i = 1;i <= Q;i++){
if (ans[i] == -69) continue;
if (ans[i] >= 0) cout<<ans[i]<<"\n";
else{
if (ans[i] == -2) cout<<"no";
else cout<<"yes"; cout<<"\n";
}
}
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:117:13: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
117 | else cout<<"yes"; cout<<"\n";
| ^~~~
servers.cpp:117:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
117 | else cout<<"yes"; cout<<"\n";
| ^~~~
servers.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
servers.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24296 KB |
Output is correct |
2 |
Correct |
41 ms |
24644 KB |
Output is correct |
3 |
Correct |
45 ms |
24548 KB |
Output is correct |
4 |
Correct |
41 ms |
24632 KB |
Output is correct |
5 |
Correct |
42 ms |
24528 KB |
Output is correct |
6 |
Correct |
42 ms |
24324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24296 KB |
Output is correct |
2 |
Correct |
41 ms |
24644 KB |
Output is correct |
3 |
Correct |
45 ms |
24548 KB |
Output is correct |
4 |
Correct |
41 ms |
24632 KB |
Output is correct |
5 |
Correct |
42 ms |
24528 KB |
Output is correct |
6 |
Correct |
42 ms |
24324 KB |
Output is correct |
7 |
Correct |
32 ms |
24200 KB |
Output is correct |
8 |
Correct |
43 ms |
24264 KB |
Output is correct |
9 |
Correct |
42 ms |
24080 KB |
Output is correct |
10 |
Correct |
45 ms |
24260 KB |
Output is correct |
11 |
Correct |
45 ms |
24076 KB |
Output is correct |
12 |
Correct |
42 ms |
23932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
24260 KB |
Output is correct |
2 |
Correct |
167 ms |
32128 KB |
Output is correct |
3 |
Correct |
178 ms |
32624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
24260 KB |
Output is correct |
2 |
Correct |
167 ms |
32128 KB |
Output is correct |
3 |
Correct |
178 ms |
32624 KB |
Output is correct |
4 |
Correct |
33 ms |
24380 KB |
Output is correct |
5 |
Correct |
161 ms |
32636 KB |
Output is correct |
6 |
Correct |
109 ms |
31260 KB |
Output is correct |
7 |
Correct |
114 ms |
31256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24392 KB |
Output is correct |
2 |
Correct |
321 ms |
40644 KB |
Output is correct |
3 |
Correct |
253 ms |
41396 KB |
Output is correct |
4 |
Correct |
196 ms |
41368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24392 KB |
Output is correct |
2 |
Correct |
321 ms |
40644 KB |
Output is correct |
3 |
Correct |
253 ms |
41396 KB |
Output is correct |
4 |
Correct |
196 ms |
41368 KB |
Output is correct |
5 |
Correct |
32 ms |
24268 KB |
Output is correct |
6 |
Correct |
266 ms |
41768 KB |
Output is correct |
7 |
Correct |
233 ms |
41972 KB |
Output is correct |
8 |
Correct |
244 ms |
41360 KB |
Output is correct |
9 |
Correct |
265 ms |
41268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24272 KB |
Output is correct |
2 |
Correct |
190 ms |
31980 KB |
Output is correct |
3 |
Correct |
196 ms |
33092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24272 KB |
Output is correct |
2 |
Correct |
190 ms |
31980 KB |
Output is correct |
3 |
Correct |
196 ms |
33092 KB |
Output is correct |
4 |
Correct |
33 ms |
24240 KB |
Output is correct |
5 |
Correct |
231 ms |
33368 KB |
Output is correct |
6 |
Correct |
217 ms |
33152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24244 KB |
Output is correct |
2 |
Correct |
242 ms |
40728 KB |
Output is correct |
3 |
Correct |
260 ms |
41424 KB |
Output is correct |
4 |
Correct |
199 ms |
41324 KB |
Output is correct |
5 |
Correct |
32 ms |
24432 KB |
Output is correct |
6 |
Correct |
196 ms |
33012 KB |
Output is correct |
7 |
Correct |
192 ms |
33116 KB |
Output is correct |
8 |
Correct |
200 ms |
33476 KB |
Output is correct |
9 |
Correct |
239 ms |
33756 KB |
Output is correct |
10 |
Correct |
291 ms |
37444 KB |
Output is correct |
11 |
Correct |
304 ms |
35832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24244 KB |
Output is correct |
2 |
Correct |
242 ms |
40728 KB |
Output is correct |
3 |
Correct |
260 ms |
41424 KB |
Output is correct |
4 |
Correct |
199 ms |
41324 KB |
Output is correct |
5 |
Correct |
32 ms |
24432 KB |
Output is correct |
6 |
Correct |
196 ms |
33012 KB |
Output is correct |
7 |
Correct |
192 ms |
33116 KB |
Output is correct |
8 |
Correct |
200 ms |
33476 KB |
Output is correct |
9 |
Correct |
239 ms |
33756 KB |
Output is correct |
10 |
Correct |
291 ms |
37444 KB |
Output is correct |
11 |
Correct |
304 ms |
35832 KB |
Output is correct |
12 |
Correct |
39 ms |
24280 KB |
Output is correct |
13 |
Correct |
263 ms |
41796 KB |
Output is correct |
14 |
Correct |
222 ms |
41840 KB |
Output is correct |
15 |
Correct |
292 ms |
41412 KB |
Output is correct |
16 |
Correct |
244 ms |
41268 KB |
Output is correct |
17 |
Correct |
33 ms |
24900 KB |
Output is correct |
18 |
Correct |
216 ms |
33252 KB |
Output is correct |
19 |
Correct |
202 ms |
33280 KB |
Output is correct |
20 |
Correct |
213 ms |
33760 KB |
Output is correct |
21 |
Correct |
224 ms |
34224 KB |
Output is correct |
22 |
Correct |
329 ms |
36544 KB |
Output is correct |
23 |
Correct |
410 ms |
37396 KB |
Output is correct |
24 |
Correct |
382 ms |
36624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24268 KB |
Output is correct |
2 |
Correct |
43 ms |
24652 KB |
Output is correct |
3 |
Correct |
40 ms |
24504 KB |
Output is correct |
4 |
Correct |
42 ms |
24632 KB |
Output is correct |
5 |
Correct |
43 ms |
24524 KB |
Output is correct |
6 |
Correct |
42 ms |
24388 KB |
Output is correct |
7 |
Correct |
32 ms |
24368 KB |
Output is correct |
8 |
Correct |
160 ms |
32088 KB |
Output is correct |
9 |
Correct |
164 ms |
32848 KB |
Output is correct |
10 |
Correct |
32 ms |
24388 KB |
Output is correct |
11 |
Correct |
242 ms |
41456 KB |
Output is correct |
12 |
Correct |
242 ms |
41540 KB |
Output is correct |
13 |
Correct |
220 ms |
41436 KB |
Output is correct |
14 |
Correct |
33 ms |
24416 KB |
Output is correct |
15 |
Correct |
197 ms |
32848 KB |
Output is correct |
16 |
Correct |
198 ms |
33100 KB |
Output is correct |
17 |
Correct |
212 ms |
33400 KB |
Output is correct |
18 |
Correct |
211 ms |
33860 KB |
Output is correct |
19 |
Correct |
269 ms |
37420 KB |
Output is correct |
20 |
Correct |
295 ms |
35848 KB |
Output is correct |
21 |
Correct |
190 ms |
32800 KB |
Output is correct |
22 |
Correct |
186 ms |
32904 KB |
Output is correct |
23 |
Correct |
217 ms |
33348 KB |
Output is correct |
24 |
Correct |
185 ms |
33360 KB |
Output is correct |
25 |
Correct |
271 ms |
37196 KB |
Output is correct |
26 |
Correct |
226 ms |
32196 KB |
Output is correct |
27 |
Correct |
214 ms |
32068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24268 KB |
Output is correct |
2 |
Correct |
43 ms |
24652 KB |
Output is correct |
3 |
Correct |
40 ms |
24504 KB |
Output is correct |
4 |
Correct |
42 ms |
24632 KB |
Output is correct |
5 |
Correct |
43 ms |
24524 KB |
Output is correct |
6 |
Correct |
42 ms |
24388 KB |
Output is correct |
7 |
Correct |
32 ms |
24368 KB |
Output is correct |
8 |
Correct |
160 ms |
32088 KB |
Output is correct |
9 |
Correct |
164 ms |
32848 KB |
Output is correct |
10 |
Correct |
32 ms |
24388 KB |
Output is correct |
11 |
Correct |
242 ms |
41456 KB |
Output is correct |
12 |
Correct |
242 ms |
41540 KB |
Output is correct |
13 |
Correct |
220 ms |
41436 KB |
Output is correct |
14 |
Correct |
33 ms |
24416 KB |
Output is correct |
15 |
Correct |
197 ms |
32848 KB |
Output is correct |
16 |
Correct |
198 ms |
33100 KB |
Output is correct |
17 |
Correct |
212 ms |
33400 KB |
Output is correct |
18 |
Correct |
211 ms |
33860 KB |
Output is correct |
19 |
Correct |
269 ms |
37420 KB |
Output is correct |
20 |
Correct |
295 ms |
35848 KB |
Output is correct |
21 |
Correct |
190 ms |
32800 KB |
Output is correct |
22 |
Correct |
186 ms |
32904 KB |
Output is correct |
23 |
Correct |
217 ms |
33348 KB |
Output is correct |
24 |
Correct |
185 ms |
33360 KB |
Output is correct |
25 |
Correct |
271 ms |
37196 KB |
Output is correct |
26 |
Correct |
226 ms |
32196 KB |
Output is correct |
27 |
Correct |
214 ms |
32068 KB |
Output is correct |
28 |
Correct |
33 ms |
24332 KB |
Output is correct |
29 |
Correct |
45 ms |
24884 KB |
Output is correct |
30 |
Correct |
47 ms |
24796 KB |
Output is correct |
31 |
Correct |
45 ms |
24988 KB |
Output is correct |
32 |
Correct |
44 ms |
24832 KB |
Output is correct |
33 |
Correct |
41 ms |
24636 KB |
Output is correct |
34 |
Correct |
33 ms |
24888 KB |
Output is correct |
35 |
Correct |
186 ms |
32600 KB |
Output is correct |
36 |
Correct |
121 ms |
31224 KB |
Output is correct |
37 |
Correct |
115 ms |
31240 KB |
Output is correct |
38 |
Correct |
32 ms |
24900 KB |
Output is correct |
39 |
Correct |
245 ms |
41792 KB |
Output is correct |
40 |
Correct |
234 ms |
42040 KB |
Output is correct |
41 |
Correct |
248 ms |
41344 KB |
Output is correct |
42 |
Correct |
239 ms |
41284 KB |
Output is correct |
43 |
Correct |
33 ms |
24960 KB |
Output is correct |
44 |
Correct |
213 ms |
33288 KB |
Output is correct |
45 |
Correct |
203 ms |
33328 KB |
Output is correct |
46 |
Correct |
209 ms |
33860 KB |
Output is correct |
47 |
Correct |
228 ms |
34264 KB |
Output is correct |
48 |
Correct |
363 ms |
36416 KB |
Output is correct |
49 |
Correct |
424 ms |
37468 KB |
Output is correct |
50 |
Correct |
375 ms |
36584 KB |
Output is correct |
51 |
Correct |
138 ms |
31468 KB |
Output is correct |
52 |
Correct |
116 ms |
31364 KB |
Output is correct |
53 |
Correct |
115 ms |
31108 KB |
Output is correct |
54 |
Correct |
117 ms |
31280 KB |
Output is correct |
55 |
Correct |
117 ms |
31296 KB |
Output is correct |
56 |
Correct |
185 ms |
32628 KB |
Output is correct |
57 |
Correct |
220 ms |
35316 KB |
Output is correct |
58 |
Correct |
300 ms |
32904 KB |
Output is correct |
59 |
Correct |
227 ms |
32140 KB |
Output is correct |