#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
#define maxN 150005
#define INF LLONG_MAX
#define INT 1000000001
using namespace std;
vector <int> adj[maxN];
long long N,P,s,i,dis[2][maxN],k[2][maxN],c[2];
bool mar[maxN];
long long dfs(int n,int d){
long long y;
if(d>2*N) return INT;
if(adj[n].size()>0 && (adj[n][0]!=s || adj[n].size()==1)) {if(dis[0][n]!=INF) return dis[0][n];}
else {if(dis[1][n]!=INF) return dis[1][n];}
if(adj[n].size()>0 && (adj[n][0]!=s || adj[n].size()==1)) y=adj[n][0];
else y=adj[n][1];
if(adj[n][0]!=s || adj[n].size()==1) {
s=n;
dis[0][n]=dfs(y,d+1)+1;
if(n==adj[y][0]) k[0][n]=k[1][y];
else k[0][n]=k[0][y];
if(adj[n].size()==1){
k[1][n]=k[0][n];
dis[1][n]=dis[0][n];
}
return dis[0][n];}
else {
s=n;
dis[1][n]=dfs(y,d+1)+1;
if(n==adj[y][0]) k[1][n]=k[1][y];
else k[1][n]=k[0][y];
return dis[1][n];}
}
long long period(int x,int d){
if(d>3*N) return INF;
int y;
if(adj[x].size()>0 && (adj[x][0]!=s || adj[x].size()==1)) y=adj[x][0];
else y=adj[x][1];
if(y==P) {
if(x==adj[P][0]) {if(c[0]==-5) c[0]=1; else c[1]=1;}
else {if(c[0]==-5) c[0]=0; else c[1]=0;}
return d+1;
}
s=x;
return period(y,d+1);
}
void count_routes(int n,int m,int p,int r[][2],int q,int g[]){
N=n;
P=p;
for(i=0;i<m;i++){
adj[r[i][0]].push_back(r[i][1]);
adj[r[i][1]].push_back(r[i][0]);
}
for(i=0;i<n;i++){
dis[0][i]=dis[1][i]=INF;
}
dis[0][p]=0;
dis[1][p]=0;
k[0][p]=0;
k[1][p]=1;
for(i=0;i<n;i++){
s=-5;
dis[0][i]=dfs(i,0);
s=adj[i][0];
dis[1][i]=dfs(i,0);
}
long long t1,t2;
s=-5;
c[0]=-5;
t1=period(p,0);
if(adj[p].size()>0) {
s=adj[p][0];
t2=period(p,0);
}
else t2=INF;
for(i=0;i<q;i++){
long long ans=0;
for(int j=0;j<n;j++){
if(k[0][j]==0 && dis[0][j]<=g[i]){
if(dis[0][j]==g[i]) {ans++; continue;}
if(c[0]==0){
if(t1!=INF && (g[i]-dis[0][j])%t1==0) {ans++; continue;}}
else{
if(c[1]==1){
if((g[i]-dis[0][j]-t1)%t2==0) {ans++; continue;}}
else{
if(t1!=INF && t2!=INF){
if((g[i]-dis[0][j])%(t1+t2)==0 || (g[i]-dis[0][j])%(t1+t2)==t1) {ans++; continue;}}
}
}
}
if(k[0][j]==1 && dis[0][j]<=g[i]){
if(dis[0][j]==g[i]) {ans++; continue;}
if(c[1]==1){
if(t2!=INF && (g[i]-dis[0][j])%t2==0) {ans++; continue;}}
else{
if(c[0]==0){
if((g[i]-dis[0][j]-t2)%t1==0) {ans++; continue;}}
else{
if(t1!=INF && t2!=INF){
if((g[i]-dis[0][j])%(t1+t2)==0 || (g[i]-dis[0][j])%(t1+t2)==t2) {ans++; continue;}}
}
}
}
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4008 KB |
Output is correct |
2 |
Correct |
1 ms |
3960 KB |
Output is correct |
3 |
Correct |
6 ms |
3960 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3932 KB |
Output is correct |
6 |
Correct |
5 ms |
4192 KB |
Output is correct |
7 |
Correct |
5 ms |
3900 KB |
Output is correct |
8 |
Correct |
5 ms |
3932 KB |
Output is correct |
9 |
Correct |
13 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4008 KB |
Output is correct |
2 |
Correct |
1 ms |
3960 KB |
Output is correct |
3 |
Correct |
6 ms |
3960 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3932 KB |
Output is correct |
6 |
Correct |
5 ms |
4192 KB |
Output is correct |
7 |
Correct |
5 ms |
3900 KB |
Output is correct |
8 |
Correct |
5 ms |
3932 KB |
Output is correct |
9 |
Correct |
13 ms |
4188 KB |
Output is correct |
10 |
Correct |
6 ms |
3888 KB |
Output is correct |
11 |
Correct |
30 ms |
8472 KB |
Output is correct |
12 |
Correct |
59 ms |
11640 KB |
Output is correct |
13 |
Correct |
46 ms |
20808 KB |
Output is correct |
14 |
Correct |
185 ms |
30756 KB |
Output is correct |
15 |
Correct |
112 ms |
14012 KB |
Output is correct |
16 |
Correct |
128 ms |
22816 KB |
Output is correct |
17 |
Correct |
90 ms |
10484 KB |
Output is correct |
18 |
Correct |
52 ms |
11524 KB |
Output is correct |
19 |
Correct |
234 ms |
30912 KB |
Output is correct |
20 |
Correct |
157 ms |
14200 KB |
Output is correct |
21 |
Correct |
128 ms |
22984 KB |
Output is correct |
22 |
Correct |
152 ms |
10728 KB |
Output is correct |
23 |
Correct |
369 ms |
33864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4008 KB |
Output is correct |
2 |
Correct |
1 ms |
3960 KB |
Output is correct |
3 |
Correct |
6 ms |
3960 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3932 KB |
Output is correct |
6 |
Correct |
5 ms |
4192 KB |
Output is correct |
7 |
Correct |
5 ms |
3900 KB |
Output is correct |
8 |
Correct |
5 ms |
3932 KB |
Output is correct |
9 |
Correct |
13 ms |
4188 KB |
Output is correct |
10 |
Correct |
6 ms |
3888 KB |
Output is correct |
11 |
Correct |
30 ms |
8472 KB |
Output is correct |
12 |
Correct |
59 ms |
11640 KB |
Output is correct |
13 |
Correct |
46 ms |
20808 KB |
Output is correct |
14 |
Correct |
185 ms |
30756 KB |
Output is correct |
15 |
Correct |
112 ms |
14012 KB |
Output is correct |
16 |
Correct |
128 ms |
22816 KB |
Output is correct |
17 |
Correct |
90 ms |
10484 KB |
Output is correct |
18 |
Correct |
52 ms |
11524 KB |
Output is correct |
19 |
Correct |
234 ms |
30912 KB |
Output is correct |
20 |
Correct |
157 ms |
14200 KB |
Output is correct |
21 |
Correct |
128 ms |
22984 KB |
Output is correct |
22 |
Correct |
152 ms |
10728 KB |
Output is correct |
23 |
Correct |
369 ms |
33864 KB |
Output is correct |
24 |
Correct |
6 ms |
3932 KB |
Output is correct |
25 |
Correct |
148 ms |
8472 KB |
Output is correct |
26 |
Correct |
164 ms |
11620 KB |
Output is correct |
27 |
Correct |
2402 ms |
20588 KB |
Output is correct |
28 |
Correct |
1661 ms |
32284 KB |
Output is correct |
29 |
Correct |
4041 ms |
15624 KB |
Output is correct |
30 |
Correct |
2638 ms |
24312 KB |
Output is correct |
31 |
Correct |
2317 ms |
12024 KB |
Output is correct |
32 |
Correct |
170 ms |
12152 KB |
Output is correct |
33 |
Correct |
1716 ms |
32268 KB |
Output is correct |
34 |
Correct |
4022 ms |
15616 KB |
Output is correct |
35 |
Correct |
2657 ms |
24212 KB |
Output is correct |
36 |
Correct |
2837 ms |
12024 KB |
Output is correct |
37 |
Correct |
1264 ms |
35320 KB |
Output is correct |
38 |
Execution timed out |
5056 ms |
21692 KB |
Time limit exceeded |