#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{
//printf("%d ",v.back());
cycle[v.back()]=C;
pos[v.back()]=++tmp;
sz[C]++;
v.ppb();
}while(v.back()!=nd);
//puts("");
}
return;
}vis[nd]=in[nd]=1;
dfs(go[nd]);
in[nd]=0;
}
/*
int dest,dest1;
int brute(int x,int rem){
if(!rem)
return (x==dest or x==dest1);
return brute(go[x],rem-1);
}*/
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[]){
//dest=P;dest1=P+N;
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);
}
//printf("EDGES\n");
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;
//printf("%d %d\n",i+j*N,to+N);
inv[to+N].pb(i+j*N);
}
else{
go[i+j*N]=to;
//printf("%d %d\n",i+j*N,to);
inv[to].pb(i+j*N);
}
}
//wr
/*puts("ANSWER:");
for(int i=0;i<N;i++)
if(brute(i,100))
printf("%d ",i);
puts("");*/
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);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14572 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
12 ms |
14700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14572 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
12 ms |
14700 KB |
Output is correct |
10 |
Correct |
10 ms |
14572 KB |
Output is correct |
11 |
Correct |
24 ms |
18156 KB |
Output is correct |
12 |
Correct |
56 ms |
20056 KB |
Output is correct |
13 |
Correct |
65 ms |
37952 KB |
Output is correct |
14 |
Correct |
176 ms |
32748 KB |
Output is correct |
15 |
Correct |
195 ms |
33036 KB |
Output is correct |
16 |
Correct |
149 ms |
27628 KB |
Output is correct |
17 |
Correct |
135 ms |
25708 KB |
Output is correct |
18 |
Correct |
49 ms |
20204 KB |
Output is correct |
19 |
Correct |
181 ms |
32748 KB |
Output is correct |
20 |
Correct |
192 ms |
33004 KB |
Output is correct |
21 |
Correct |
153 ms |
27756 KB |
Output is correct |
22 |
Correct |
125 ms |
25836 KB |
Output is correct |
23 |
Correct |
190 ms |
34924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14572 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
12 ms |
14700 KB |
Output is correct |
10 |
Correct |
10 ms |
14572 KB |
Output is correct |
11 |
Correct |
24 ms |
18156 KB |
Output is correct |
12 |
Correct |
56 ms |
20056 KB |
Output is correct |
13 |
Correct |
65 ms |
37952 KB |
Output is correct |
14 |
Correct |
176 ms |
32748 KB |
Output is correct |
15 |
Correct |
195 ms |
33036 KB |
Output is correct |
16 |
Correct |
149 ms |
27628 KB |
Output is correct |
17 |
Correct |
135 ms |
25708 KB |
Output is correct |
18 |
Correct |
49 ms |
20204 KB |
Output is correct |
19 |
Correct |
181 ms |
32748 KB |
Output is correct |
20 |
Correct |
192 ms |
33004 KB |
Output is correct |
21 |
Correct |
153 ms |
27756 KB |
Output is correct |
22 |
Correct |
125 ms |
25836 KB |
Output is correct |
23 |
Correct |
190 ms |
34924 KB |
Output is correct |
24 |
Correct |
12 ms |
14572 KB |
Output is correct |
25 |
Correct |
296 ms |
18284 KB |
Output is correct |
26 |
Correct |
478 ms |
20076 KB |
Output is correct |
27 |
Correct |
2589 ms |
38888 KB |
Output is correct |
28 |
Correct |
2416 ms |
34540 KB |
Output is correct |
29 |
Correct |
3729 ms |
34824 KB |
Output is correct |
30 |
Correct |
2082 ms |
29420 KB |
Output is correct |
31 |
Correct |
2341 ms |
27288 KB |
Output is correct |
32 |
Correct |
761 ms |
20716 KB |
Output is correct |
33 |
Correct |
2406 ms |
34444 KB |
Output is correct |
34 |
Correct |
3999 ms |
34928 KB |
Output is correct |
35 |
Correct |
3125 ms |
29292 KB |
Output is correct |
36 |
Correct |
2023 ms |
27500 KB |
Output is correct |
37 |
Correct |
2218 ms |
36844 KB |
Output is correct |
38 |
Correct |
3267 ms |
49892 KB |
Output is correct |