#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 = 2e5 + 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;
//debug(ans[6]);
for (auto i : g[cent])
if (i.sc != par && !ban[i.sc]) solve(i.sc,now);
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
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);
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:118:13: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
118 | else cout<<"yes"; cout<<"\n";
| ^~~~
servers.cpp:118:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
118 | else cout<<"yes"; cout<<"\n";
| ^~~~
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".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
17356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
17356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
17348 KB |
Output is correct |
2 |
Incorrect |
147 ms |
23932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
17348 KB |
Output is correct |
2 |
Incorrect |
147 ms |
23932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
17292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
17292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
17288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
17288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
17248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
17248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
17212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
17212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |