This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |