#include "garden.h"
#include "gardenlib.h"
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<deque>
#include<vector>
int a,b,c,t,h,p,q,check,p1,q1,y,z,sz1,sz2,v[150003][2],ch2[150003][2],ch1[150003][2],x[150003][2],D1[150003][2],D2[150003][2],cycle1[150003][2],cycle2[150003][2];
void count_routes(int N,int M,int P,int R[][2],int Q,int G[])
{
a=N;b=M;
c=P+1;
for(int i=0;i<b;i++)
{
if(x[R[i][0]+1][0]==0)x[R[i][0]+1][0]=R[i][1]+1;
else if(x[R[i][0]+1][1]==0)x[R[i][0]+1][1]=R[i][1]+1;
if(x[R[i][1]+1][0]==0)x[R[i][1]+1][0]=R[i][0]+1;
else if(x[R[i][1]+1][1]==0)x[R[i][1]+1][1]=R[i][0]+1;
}
for(int i=1;i<=a;i++)
{
if(x[i][1]==0)x[i][1]=x[i][0];
}
p=c;q=0;t=0;
D1[p][q]=0;
ch1[p][q]=1;
v[p][q]=1;
p1=-1;q1=-1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
if(p1==c&&q1==0)break;
else
{
if(v[p1][q1]==1)break;
v[p1][q1]=1;
p=p1;q=q1;
t++;
}
}
if(p1==c&&q1==0)
{
p=c;q=0;
cycle1[p][q]=t+1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
if(p1==c&&q1==0)break;
p=p1;q=q1;
ch1[p][q]=1;
D1[p][q]=t;t--;
cycle1[p][q]=cycle1[c][0];
}
}
memset(v,0,sizeof(v));
p=c;q=1;t=0;
D2[p][q]=0;
ch2[p][q]=1;
v[p][q]=1;
p1=-1;q1=-1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
if(p1==c&&q1==1)break;
else
{
if(v[p1][q1]==1)break;
v[p1][q1]=1;
p=p1;q=q1;
t++;
}
}
if(p1==c&&q1==1)
{
p=c;q=1;
cycle2[p][q]=t+1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
if(p1==c&&q1==1)break;
p=p1;q=q1;
ch2[p][q]=1;
D2[p][q]=t;
cycle2[p][q]=cycle2[c][1];
t--;
}
}
memset(v,0,sizeof(v));
for(int i=1;i<=a;i++)
{
for(int j=0;j<2;j++)
{
if(ch1[i][j])continue;
p=i;q=j;t=0;
v[i][j]=1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
t++;
if(ch1[p1][q1])break;
else if(v[p1][q1])break;
p=p1;q=q1;
v[p][q]=1;
}
if(ch1[p1][q1])
{
y=p1;z=q1;
p=i;q=j;
ch1[p][q]=1;
D1[p][q]=D1[y][z]+t;
cycle1[p][q]=cycle1[y][z];
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
t--;
if(p1==y&&q1==z)break;
p=p1;q=q1;
ch1[p][q]=1;
D1[p][q]=D1[y][z]+t;
cycle1[p][q]=cycle1[y][z];
}
}
}
}
memset(v,0,sizeof(v));
for(int i=1;i<=a;i++)
{
for(int j=0;j<2;j++)
{
if(ch2[i][j])continue;
p=i;q=j;t=0;
v[i][j]=1;
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
t++;
if(ch2[p1][q1])break;
else if(v[p1][q1])break;
p=p1;q=q1;
v[p][q]=1;
}
if(ch2[p1][q1])
{
y=p1;z=q1;
p=i;q=j;
ch2[p][q]=1;
D2[p][q]=D2[y][z]+t;
cycle2[p][q]=cycle2[y][z];
while(1)
{
if(q==0)p1=x[p][0];
else p1=x[p][1];
if(p==x[p1][0])q1=1;
else q1=0;
t--;
if(p1==y&&q1==z)break;
p=p1;q=q1;
ch2[p][q]=1;
D2[p][q]=D2[y][z]+t;
cycle2[p][q]=cycle2[y][z];
}
}
}
}
int ans;
for(int i=0;i<Q;i++)
{
ans=0;
for(int j=1;j<=a;j++)
{
if(ch1[j][0]&&(D1[j][0]==G[i]||(cycle1[j][0]&&G[i]>D1[j][0]&&(G[i]-D1[j][0])%cycle1[j][0]==0)))
{
ans++;
}
else if(ch2[j][0]&&(D2[j][0]==G[i]||(cycle2[j][0]&&G[i]>D2[j][0]&&(G[i]-D2[j][0])%cycle2[j][0]==0)))
{
ans++;
}
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1532 KB |
Output is correct |
3 |
Correct |
3 ms |
1628 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1524 KB |
Output is correct |
8 |
Correct |
3 ms |
1528 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1532 KB |
Output is correct |
3 |
Correct |
3 ms |
1628 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1524 KB |
Output is correct |
8 |
Correct |
3 ms |
1528 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
3 ms |
1500 KB |
Output is correct |
11 |
Correct |
10 ms |
2144 KB |
Output is correct |
12 |
Correct |
20 ms |
2988 KB |
Output is correct |
13 |
Correct |
30 ms |
6904 KB |
Output is correct |
14 |
Correct |
59 ms |
8616 KB |
Output is correct |
15 |
Correct |
69 ms |
11232 KB |
Output is correct |
16 |
Correct |
58 ms |
7136 KB |
Output is correct |
17 |
Correct |
51 ms |
8312 KB |
Output is correct |
18 |
Correct |
21 ms |
3676 KB |
Output is correct |
19 |
Correct |
69 ms |
11092 KB |
Output is correct |
20 |
Correct |
68 ms |
11892 KB |
Output is correct |
21 |
Correct |
54 ms |
7184 KB |
Output is correct |
22 |
Correct |
53 ms |
8308 KB |
Output is correct |
23 |
Correct |
60 ms |
9104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1532 KB |
Output is correct |
3 |
Correct |
3 ms |
1628 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1524 KB |
Output is correct |
8 |
Correct |
3 ms |
1528 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
3 ms |
1500 KB |
Output is correct |
11 |
Correct |
10 ms |
2144 KB |
Output is correct |
12 |
Correct |
20 ms |
2988 KB |
Output is correct |
13 |
Correct |
30 ms |
6904 KB |
Output is correct |
14 |
Correct |
59 ms |
8616 KB |
Output is correct |
15 |
Correct |
69 ms |
11232 KB |
Output is correct |
16 |
Correct |
58 ms |
7136 KB |
Output is correct |
17 |
Correct |
51 ms |
8312 KB |
Output is correct |
18 |
Correct |
21 ms |
3676 KB |
Output is correct |
19 |
Correct |
69 ms |
11092 KB |
Output is correct |
20 |
Correct |
68 ms |
11892 KB |
Output is correct |
21 |
Correct |
54 ms |
7184 KB |
Output is correct |
22 |
Correct |
53 ms |
8308 KB |
Output is correct |
23 |
Correct |
60 ms |
9104 KB |
Output is correct |
24 |
Correct |
4 ms |
1528 KB |
Output is correct |
25 |
Correct |
94 ms |
2508 KB |
Output is correct |
26 |
Correct |
128 ms |
3720 KB |
Output is correct |
27 |
Correct |
1685 ms |
7928 KB |
Output is correct |
28 |
Correct |
1195 ms |
11240 KB |
Output is correct |
29 |
Correct |
1903 ms |
11384 KB |
Output is correct |
30 |
Correct |
1039 ms |
7176 KB |
Output is correct |
31 |
Correct |
1069 ms |
8020 KB |
Output is correct |
32 |
Correct |
145 ms |
3716 KB |
Output is correct |
33 |
Correct |
1216 ms |
9980 KB |
Output is correct |
34 |
Correct |
1844 ms |
11644 KB |
Output is correct |
35 |
Correct |
1034 ms |
7684 KB |
Output is correct |
36 |
Correct |
1626 ms |
8276 KB |
Output is correct |
37 |
Correct |
803 ms |
8148 KB |
Output is correct |
38 |
Correct |
2725 ms |
10940 KB |
Output is correct |