#include "garden.h"
#include "gardenlib.h"
#include "bits/stdc++.h"
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ppb() pop_back()
#define MAXN 300009
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
int go[MAXN],C,sz[MAXN],lvl[MAXN],who[MAXN],pos[MAXN];
int vis[MAXN],cycle[MAXN],in[MAXN],ata[MAXN];
vector<int>inv[MAXN],v,adj[MAXN];
int tin[MAXN],tout[MAXN],T;
void dfs(int nd){
v.pb(nd);
if(vis[nd]){
if(in[nd]){
int tmp=0;
do{
cycle[v.back()]=C;
pos[v.back()]=++tmp;
sz[C]++;
v.ppb();
}while(v.back()!=nd);
}
return;
}vis[nd]=in[nd]=1;
dfs(go[nd]);
in[nd]=0;
}
int dis(int x,int y,int id){
if(y<=x)
return x-y;
return x+sz[id]-y;
}
int baba(int x,int y){
return (tin[x]<=tin[y] and tout[y]<=tout[x]);
}
bool check(int src,int dest,int step){
if(who[src]!=who[dest])
return 0;
if(cycle[src] and !cycle[dest])
return 0;
if(!cycle[src]){
if(!cycle[dest]){
if(lvl[src]-lvl[dest]==step and baba(dest,src))
return 1;
}
else{
step-=lvl[src];
step-=dis(pos[ata[src]],pos[dest],cycle[dest]);
if(step>=0 and step%sz[cycle[dest]]==0)
return 1;
}
}
else{
step-=dis(pos[src],pos[dest],cycle[dest]);
if(step>=0 and step%sz[cycle[dest]]==0)
return 1;
}
return 0;
}
void dfs1(int nd){
tin[nd]=++T;
tr(it,inv[nd])
if(!cycle[*it]){
lvl[*it]=lvl[nd]+1;
ata[*it]=ata[nd];
who[*it]=who[nd];
dfs1(*it);
}
tout[nd]=T;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
for(int i=0;i<M;i++){
int x=R[i][0],y=R[i][1];
if(adj[x].size()<2)adj[x].push_back(y);
if(adj[y].size()<2)adj[y].push_back(x);
}
for(int i=0;i<N;i++)
for(int j=0;j<(int)adj[i].size();j++){
int to=adj[i][j];
if(adj[to][0]==i and adj[to].size()>1){
go[i+j*N]=to+N;
inv[to+N].pb(i+j*N);
}
else{
go[i+j*N]=to;
inv[to].pb(i+j*N);
}
}
for(int i=0;i<2*N;i++)
if(!vis[i]){
C++;dfs(i);
v.clear();
}
for(int i=0;i<2*N;i++)
if(cycle[i]){
ata[i]=i;who[i]=cycle[i];
dfs1(i);
}
for(int i=0;i<Q;i++){
int res=0,ok;
for(int j=0;j<N;j++){
ok=check(j,P,G[i]);
ok|=check(j,P+N,G[i]);
res+=ok;
}
answer(res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14700 KB |
Output is correct |
2 |
Correct |
11 ms |
14700 KB |
Output is correct |
3 |
Correct |
12 ms |
14700 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
13 ms |
14828 KB |
Output is correct |
7 |
Correct |
10 ms |
14572 KB |
Output is correct |
8 |
Correct |
11 ms |
14700 KB |
Output is correct |
9 |
Correct |
13 ms |
14700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14700 KB |
Output is correct |
2 |
Correct |
11 ms |
14700 KB |
Output is correct |
3 |
Correct |
12 ms |
14700 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
13 ms |
14828 KB |
Output is correct |
7 |
Correct |
10 ms |
14572 KB |
Output is correct |
8 |
Correct |
11 ms |
14700 KB |
Output is correct |
9 |
Correct |
13 ms |
14700 KB |
Output is correct |
10 |
Correct |
11 ms |
14572 KB |
Output is correct |
11 |
Correct |
27 ms |
18284 KB |
Output is correct |
12 |
Correct |
58 ms |
20744 KB |
Output is correct |
13 |
Correct |
69 ms |
38888 KB |
Output is correct |
14 |
Correct |
186 ms |
33900 KB |
Output is correct |
15 |
Correct |
207 ms |
34488 KB |
Output is correct |
16 |
Correct |
149 ms |
29164 KB |
Output is correct |
17 |
Correct |
133 ms |
27244 KB |
Output is correct |
18 |
Correct |
56 ms |
20716 KB |
Output is correct |
19 |
Correct |
202 ms |
34284 KB |
Output is correct |
20 |
Correct |
198 ms |
34524 KB |
Output is correct |
21 |
Correct |
176 ms |
29464 KB |
Output is correct |
22 |
Correct |
128 ms |
27244 KB |
Output is correct |
23 |
Correct |
194 ms |
36460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14700 KB |
Output is correct |
2 |
Correct |
11 ms |
14700 KB |
Output is correct |
3 |
Correct |
12 ms |
14700 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
13 ms |
14828 KB |
Output is correct |
7 |
Correct |
10 ms |
14572 KB |
Output is correct |
8 |
Correct |
11 ms |
14700 KB |
Output is correct |
9 |
Correct |
13 ms |
14700 KB |
Output is correct |
10 |
Correct |
11 ms |
14572 KB |
Output is correct |
11 |
Correct |
27 ms |
18284 KB |
Output is correct |
12 |
Correct |
58 ms |
20744 KB |
Output is correct |
13 |
Correct |
69 ms |
38888 KB |
Output is correct |
14 |
Correct |
186 ms |
33900 KB |
Output is correct |
15 |
Correct |
207 ms |
34488 KB |
Output is correct |
16 |
Correct |
149 ms |
29164 KB |
Output is correct |
17 |
Correct |
133 ms |
27244 KB |
Output is correct |
18 |
Correct |
56 ms |
20716 KB |
Output is correct |
19 |
Correct |
202 ms |
34284 KB |
Output is correct |
20 |
Correct |
198 ms |
34524 KB |
Output is correct |
21 |
Correct |
176 ms |
29464 KB |
Output is correct |
22 |
Correct |
128 ms |
27244 KB |
Output is correct |
23 |
Correct |
194 ms |
36460 KB |
Output is correct |
24 |
Correct |
12 ms |
14572 KB |
Output is correct |
25 |
Correct |
306 ms |
18428 KB |
Output is correct |
26 |
Correct |
482 ms |
20844 KB |
Output is correct |
27 |
Correct |
2625 ms |
39016 KB |
Output is correct |
28 |
Correct |
2419 ms |
34412 KB |
Output is correct |
29 |
Correct |
3710 ms |
34792 KB |
Output is correct |
30 |
Correct |
2099 ms |
29392 KB |
Output is correct |
31 |
Correct |
2379 ms |
27500 KB |
Output is correct |
32 |
Correct |
759 ms |
20716 KB |
Output is correct |
33 |
Correct |
2423 ms |
34420 KB |
Output is correct |
34 |
Correct |
4010 ms |
34704 KB |
Output is correct |
35 |
Correct |
3102 ms |
29324 KB |
Output is correct |
36 |
Correct |
2083 ms |
27372 KB |
Output is correct |
37 |
Correct |
2254 ms |
36588 KB |
Output is correct |
38 |
Correct |
3252 ms |
49764 KB |
Output is correct |