#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const int N=250005;
vector<vector<pair<int,int>>>adj(N);
deque<int>cur_adj[N],order;
int in[N],out[N],dep[N],p[N][19],val[N],max_inc[N],max_dec[N],T;
void init_dfs(int v,int par){
in[v]=++T;order.push_back(v);
p[v][0]=par;
for(int i=1;i<19;i++)p[v][i]=p[p[v][i-1]][i-1];
max_inc[v]=max_dec[v]=par;
if(val[v]<val[par])max_inc[v]=max_inc[par];
else max_dec[v]=max_dec[par];
for(auto e:adj[v]){
int u=e.first,w=e.second;
if(u==par)continue;
dep[u]=dep[v]+1;
val[u]=w;
init_dfs(u,v);
}
out[v]=T;
}
bool anc(int v,int u){
return in[v]<=in[u]&&out[u]<=out[v];
}
int LCA(int u,int v){
if(anc(u,v))return u;
if(anc(v,u))return v;
for(int i=18;i>=0;i--){
if(!anc(p[u][i],v))u=p[u][v];
}
return p[u][0];
}
int max_in_path(int u,int v){
if(anc(u,v))return val[v];
if(!anc(v,u))return val[u];
for(int i=18;i>=0;i--){
if(!anc(p[u][i],v))u=p[u][i];
}
return val[u];
}
bool can_reach(int u,int v,int Time){
if(u==v)return 1;
if((!anc(max_inc[u],v))||(!anc(max_dec[v],u)))return 0;
if(max_in_path(u,v)>Time)return 0;
for(int i=18;i>=0;i--){
if(!anc(p[u][i],v))u=p[u][i];
if(!anc(p[v][i],u))v=p[v][i];
}
return val[u]<val[v];
}
int dp_down[N],dp_up[N],dp[N],vis[N];
void dfs_down(int v,int par){
vis[v]=1;
dp_down[v]=dp[v]=1;
for(auto u:cur_adj[v]){
if(u==par)continue;
dfs_down(u,v);
if(val[u]>val[v])dp_down[v]+=dp_down[u];
dp[v]+=dp_down[u];
}
}
void dfs_up(int v,int par,int sum=0){
dp_up[v]=sum+(par!=v);
if(val[par]>val[v])dp_up[v]+=dp_up[par];
dp[v]+=dp_up[v];
sum=0;
for(auto u:cur_adj[v]){
if(u==par)continue;
dfs_up(u,v,sum);
sum+=dp_down[u];
}
}
void reclac(){
memset(vis,0,sizeof(vis));
for(auto u:order){
if(vis[u])continue;
dfs_down(u,u);
dfs_up(u,u);
}
}
char op[N];
int A[N],B[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<n+q;i++){
cin>>op[i];
if(op[i]=='S'){
cin>>A[i]>>B[i];
adj[A[i]].push_back({B[i],i});
adj[B[i]].push_back({A[i],i});
}
if(op[i]=='Q')cin>>A[i]>>B[i];
if(op[i]=='C')cin>>A[i];
}
init_dfs(1,1);
for(int i=1;i<n+q;i++){
if(op[i]=='S'){
cur_adj[A[i]].push_front(B[i]);
cur_adj[B[i]].push_front(A[i]);
reclac();
}else if(op[i]=='Q')cout<<(can_reach(B[i],A[i],i)?"yes\n":"no\n");
else cout<<dp[A[i]]<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
111 ms |
185936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
111 ms |
185936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
185756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
185756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
185812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
185812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
185784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
185784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
106 ms |
185688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
106 ms |
185688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
106 ms |
184876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
106 ms |
184876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |