#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
typedef long long ll;
const int N=250005,SQ=350;
vector<vector<pair<int,int>>>adj(N);
vector<int>cur_adj[N],order;
int in[N],out[N],dep[N],p[N][18],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<18;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 max_in_path(int u,int v){
if(anc(u,v))return val[v];
int pu=u,pv=v;
for(int i=17;i>=0;i--){
if(!anc(p[pu][i],v))pu=p[pu][i];
if(!anc(p[pv][i],u))pv=p[pv][i];
}
if(p[pu][0]==v)return val[pu];
return val[pu]<val[pv]?val[v]:INT_MAX;
}
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(!anc(v,u)&&val[v]>Time)return 0;
return max_in_path(u,v)<=Time;
}
int DP[N][3];
int vis[N];
#define dp_down(v) DP[v][0]
#define dp_up(v) DP[v][1]
#define dp(v) DP[v][2]
void dfs_down(int v,int par){
if(vis[v])return;
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(int i=cur_adj[v].size()-1;i>=0;i--){
int u=cur_adj[v][i];
if(u==par)continue;
dfs_up(u,v,sum);
sum+=dp_down(u);
}
}
void reclac(){
memset(vis+1,0,int(order.size())*sizeof(vis[1]));
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;i++)dp(i)=1;
int cnt=0;
vector<int>edges;
for(int i=1;i<n+q;i++){
if(cnt==SQ)reclac(),cnt=0,edges.clear();
if(op[i]=='S'){
edges.push_back(i);
cur_adj[A[i]].push_back(B[i]);
cur_adj[B[i]].push_back(A[i]);
cnt++;
}else if(op[i]=='Q')cout<<(can_reach(B[i],A[i],i)?"yes\n":"no\n");
else{
int res=dp(A[i]);
for(auto&e:edges){
int u=A[e],v=B[e];
if(val[u]!=e)swap(u,v);
if(!anc(u,A[i])&&can_reach(A[i],u,i))res++;
else if(anc(u,A[i])&&can_reach(A[i],v,i))res++;
}
cout<<res<<"\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23580 KB |
Output is correct |
2 |
Correct |
25 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
24152 KB |
Output is correct |
4 |
Correct |
34 ms |
23888 KB |
Output is correct |
5 |
Correct |
27 ms |
24144 KB |
Output is correct |
6 |
Correct |
37 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23580 KB |
Output is correct |
2 |
Correct |
25 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
24152 KB |
Output is correct |
4 |
Correct |
34 ms |
23888 KB |
Output is correct |
5 |
Correct |
27 ms |
24144 KB |
Output is correct |
6 |
Correct |
37 ms |
23900 KB |
Output is correct |
7 |
Correct |
31 ms |
23580 KB |
Output is correct |
8 |
Correct |
112 ms |
23892 KB |
Output is correct |
9 |
Correct |
338 ms |
24404 KB |
Output is correct |
10 |
Correct |
106 ms |
23888 KB |
Output is correct |
11 |
Correct |
74 ms |
24144 KB |
Output is correct |
12 |
Correct |
1012 ms |
24168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23896 KB |
Output is correct |
2 |
Correct |
763 ms |
41840 KB |
Output is correct |
3 |
Correct |
1080 ms |
41976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23896 KB |
Output is correct |
2 |
Correct |
763 ms |
41840 KB |
Output is correct |
3 |
Correct |
1080 ms |
41976 KB |
Output is correct |
4 |
Correct |
63 ms |
23716 KB |
Output is correct |
5 |
Correct |
1479 ms |
42012 KB |
Output is correct |
6 |
Correct |
1012 ms |
42440 KB |
Output is correct |
7 |
Correct |
2499 ms |
42168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23640 KB |
Output is correct |
2 |
Correct |
1234 ms |
50292 KB |
Output is correct |
3 |
Correct |
1142 ms |
50104 KB |
Output is correct |
4 |
Correct |
909 ms |
49984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23640 KB |
Output is correct |
2 |
Correct |
1234 ms |
50292 KB |
Output is correct |
3 |
Correct |
1142 ms |
50104 KB |
Output is correct |
4 |
Correct |
909 ms |
49984 KB |
Output is correct |
5 |
Correct |
25 ms |
23644 KB |
Output is correct |
6 |
Correct |
1232 ms |
50084 KB |
Output is correct |
7 |
Correct |
1366 ms |
50064 KB |
Output is correct |
8 |
Correct |
1226 ms |
49864 KB |
Output is correct |
9 |
Correct |
1573 ms |
49876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23644 KB |
Output is correct |
2 |
Correct |
613 ms |
41416 KB |
Output is correct |
3 |
Correct |
1301 ms |
41680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23644 KB |
Output is correct |
2 |
Correct |
613 ms |
41416 KB |
Output is correct |
3 |
Correct |
1301 ms |
41680 KB |
Output is correct |
4 |
Correct |
32 ms |
23644 KB |
Output is correct |
5 |
Correct |
1340 ms |
41824 KB |
Output is correct |
6 |
Correct |
1614 ms |
41468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23640 KB |
Output is correct |
2 |
Correct |
1278 ms |
50076 KB |
Output is correct |
3 |
Correct |
1051 ms |
50064 KB |
Output is correct |
4 |
Correct |
1105 ms |
49888 KB |
Output is correct |
5 |
Correct |
19 ms |
23644 KB |
Output is correct |
6 |
Correct |
566 ms |
41456 KB |
Output is correct |
7 |
Correct |
1522 ms |
41560 KB |
Output is correct |
8 |
Correct |
1288 ms |
42716 KB |
Output is correct |
9 |
Correct |
1717 ms |
42864 KB |
Output is correct |
10 |
Correct |
2080 ms |
45068 KB |
Output is correct |
11 |
Correct |
2875 ms |
44404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23640 KB |
Output is correct |
2 |
Correct |
1278 ms |
50076 KB |
Output is correct |
3 |
Correct |
1051 ms |
50064 KB |
Output is correct |
4 |
Correct |
1105 ms |
49888 KB |
Output is correct |
5 |
Correct |
19 ms |
23644 KB |
Output is correct |
6 |
Correct |
566 ms |
41456 KB |
Output is correct |
7 |
Correct |
1522 ms |
41560 KB |
Output is correct |
8 |
Correct |
1288 ms |
42716 KB |
Output is correct |
9 |
Correct |
1717 ms |
42864 KB |
Output is correct |
10 |
Correct |
2080 ms |
45068 KB |
Output is correct |
11 |
Correct |
2875 ms |
44404 KB |
Output is correct |
12 |
Correct |
25 ms |
23636 KB |
Output is correct |
13 |
Correct |
1311 ms |
49880 KB |
Output is correct |
14 |
Correct |
1494 ms |
50004 KB |
Output is correct |
15 |
Correct |
1459 ms |
49876 KB |
Output is correct |
16 |
Correct |
1243 ms |
50124 KB |
Output is correct |
17 |
Correct |
26 ms |
23640 KB |
Output is correct |
18 |
Correct |
1262 ms |
41440 KB |
Output is correct |
19 |
Correct |
1310 ms |
41400 KB |
Output is correct |
20 |
Correct |
1829 ms |
42700 KB |
Output is correct |
21 |
Correct |
1532 ms |
42868 KB |
Output is correct |
22 |
Execution timed out |
3591 ms |
44480 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23640 KB |
Output is correct |
2 |
Correct |
36 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
24144 KB |
Output is correct |
4 |
Correct |
25 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
24156 KB |
Output is correct |
6 |
Correct |
38 ms |
23892 KB |
Output is correct |
7 |
Correct |
37 ms |
23640 KB |
Output is correct |
8 |
Correct |
746 ms |
41908 KB |
Output is correct |
9 |
Correct |
750 ms |
42036 KB |
Output is correct |
10 |
Correct |
19 ms |
23644 KB |
Output is correct |
11 |
Correct |
1052 ms |
49904 KB |
Output is correct |
12 |
Correct |
1143 ms |
50116 KB |
Output is correct |
13 |
Correct |
891 ms |
50004 KB |
Output is correct |
14 |
Correct |
19 ms |
23640 KB |
Output is correct |
15 |
Correct |
620 ms |
41348 KB |
Output is correct |
16 |
Correct |
1276 ms |
41620 KB |
Output is correct |
17 |
Correct |
1294 ms |
42720 KB |
Output is correct |
18 |
Correct |
1128 ms |
42904 KB |
Output is correct |
19 |
Correct |
1624 ms |
45084 KB |
Output is correct |
20 |
Correct |
2445 ms |
44636 KB |
Output is correct |
21 |
Correct |
745 ms |
42244 KB |
Output is correct |
22 |
Correct |
700 ms |
42296 KB |
Output is correct |
23 |
Correct |
841 ms |
42656 KB |
Output is correct |
24 |
Correct |
844 ms |
42704 KB |
Output is correct |
25 |
Correct |
1081 ms |
44208 KB |
Output is correct |
26 |
Correct |
1158 ms |
41488 KB |
Output is correct |
27 |
Correct |
1122 ms |
41576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23640 KB |
Output is correct |
2 |
Correct |
36 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
24144 KB |
Output is correct |
4 |
Correct |
25 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
24156 KB |
Output is correct |
6 |
Correct |
38 ms |
23892 KB |
Output is correct |
7 |
Correct |
37 ms |
23640 KB |
Output is correct |
8 |
Correct |
746 ms |
41908 KB |
Output is correct |
9 |
Correct |
750 ms |
42036 KB |
Output is correct |
10 |
Correct |
19 ms |
23644 KB |
Output is correct |
11 |
Correct |
1052 ms |
49904 KB |
Output is correct |
12 |
Correct |
1143 ms |
50116 KB |
Output is correct |
13 |
Correct |
891 ms |
50004 KB |
Output is correct |
14 |
Correct |
19 ms |
23640 KB |
Output is correct |
15 |
Correct |
620 ms |
41348 KB |
Output is correct |
16 |
Correct |
1276 ms |
41620 KB |
Output is correct |
17 |
Correct |
1294 ms |
42720 KB |
Output is correct |
18 |
Correct |
1128 ms |
42904 KB |
Output is correct |
19 |
Correct |
1624 ms |
45084 KB |
Output is correct |
20 |
Correct |
2445 ms |
44636 KB |
Output is correct |
21 |
Correct |
745 ms |
42244 KB |
Output is correct |
22 |
Correct |
700 ms |
42296 KB |
Output is correct |
23 |
Correct |
841 ms |
42656 KB |
Output is correct |
24 |
Correct |
844 ms |
42704 KB |
Output is correct |
25 |
Correct |
1081 ms |
44208 KB |
Output is correct |
26 |
Correct |
1158 ms |
41488 KB |
Output is correct |
27 |
Correct |
1122 ms |
41576 KB |
Output is correct |
28 |
Correct |
29 ms |
23640 KB |
Output is correct |
29 |
Correct |
96 ms |
23872 KB |
Output is correct |
30 |
Correct |
324 ms |
24148 KB |
Output is correct |
31 |
Correct |
105 ms |
24052 KB |
Output is correct |
32 |
Correct |
71 ms |
24232 KB |
Output is correct |
33 |
Correct |
1000 ms |
24656 KB |
Output is correct |
34 |
Correct |
53 ms |
23636 KB |
Output is correct |
35 |
Correct |
1128 ms |
42184 KB |
Output is correct |
36 |
Correct |
846 ms |
42180 KB |
Output is correct |
37 |
Correct |
1984 ms |
42252 KB |
Output is correct |
38 |
Correct |
21 ms |
23576 KB |
Output is correct |
39 |
Correct |
1092 ms |
50460 KB |
Output is correct |
40 |
Correct |
1372 ms |
50264 KB |
Output is correct |
41 |
Correct |
1194 ms |
49868 KB |
Output is correct |
42 |
Correct |
1196 ms |
49908 KB |
Output is correct |
43 |
Correct |
24 ms |
23644 KB |
Output is correct |
44 |
Correct |
1235 ms |
41732 KB |
Output is correct |
45 |
Correct |
1182 ms |
41620 KB |
Output is correct |
46 |
Correct |
1275 ms |
42944 KB |
Output is correct |
47 |
Correct |
1227 ms |
42956 KB |
Output is correct |
48 |
Correct |
2827 ms |
44492 KB |
Output is correct |
49 |
Correct |
2232 ms |
48144 KB |
Output is correct |
50 |
Correct |
1852 ms |
48188 KB |
Output is correct |
51 |
Correct |
1470 ms |
44992 KB |
Output is correct |
52 |
Correct |
2386 ms |
45036 KB |
Output is correct |
53 |
Correct |
1996 ms |
44788 KB |
Output is correct |
54 |
Correct |
871 ms |
45028 KB |
Output is correct |
55 |
Correct |
2076 ms |
44768 KB |
Output is correct |
56 |
Correct |
888 ms |
45444 KB |
Output is correct |
57 |
Correct |
1182 ms |
46224 KB |
Output is correct |
58 |
Correct |
1371 ms |
43796 KB |
Output is correct |
59 |
Correct |
1126 ms |
44240 KB |
Output is correct |