#include "garden.h"
#include "gardenlib.h"
#include<vector>
using namespace std;
#define MAXN 151000
vector<int> connexion[MAXN];
int destination[2*MAXN];
vector<int> revdest[2*MAXN];
int dist1[2*MAXN];
int dist2[2*MAXN];
bool vst1[2*MAXN];
bool vst2[2*MAXN];
int incycle1=0;
int incycle2=0;
int PP,NN;
void dfs1(int i,int l)
{
if(i==PP && vst1[i]) incycle1=l;
if(vst1[i]) return ;
vst1[i]=true;
dist1[i]=l;
for(int x=0;x<revdest[i].size();x++)
dfs1(revdest[i][x],l+1);
return;
}
void dfs2(int i,int l)
{
if(i==PP+NN && vst2[i]) incycle2=l;
if(vst2[i]) return;
vst2[i]=true;
dist2[i]=l;
for(int x=0;x<revdest[i].size();x++)
dfs2(revdest[i][x],l+1);
return;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
NN=N;
PP=P;
for(int i=0;i<M;i++)
{
connexion[R[i][0]].push_back(R[i][1]);
connexion[R[i][1]].push_back(R[i][0]);
}
for(int i=0;i<N;i++)
{
destination[i+N]=destination[i]=connexion[i][0];
if(connexion[i].size()!=1)
destination[i+N]=connexion[i][1];
}
for(int i=0;i<2*N;i++)
{
if(connexion[destination[i]][0]==i%N)
revdest[destination[i]+N].push_back(i);
else
revdest[destination[i]].push_back(i);
}
dfs1(P,0);
dfs2(P+N,0);
/*
for(int i=0;i<2*N;i++) printf("%d ",dist1[i]);
printf("%d ",incycle1);
puts("");
for(int i=0;i<2*N;i++) printf("%d ",dist2[i]);
printf("%d ",incycle2);
*/
for(int i=0;i<Q;i++)
{
int cnt=0;
for(int j=0;j<N;j++)
{
if(G[i]>=dist1[j] && vst1[j])
{
if(incycle1==0)
{
if(G[i]==dist1[j])
{
cnt++;
continue;
}
}
else
if( (G[i]-dist1[j])%incycle1==0 )
{
cnt++;
continue;
}
}
if(G[i]>=dist2[j] && vst2[j])
{
if(incycle2==0)
{
if(G[i]==dist2[j])
cnt++;
}
else
if( (G[i]-dist2[j])%incycle2==0 )
cnt++;
}
}
answer(cnt);
}
/*
for(int i=0;i<2*N;i++)
{
fprintf(stderr,"\n%d",revdest[i].size());
for(int j=0;j<revdest[i].size();j++)
fprintf(stderr," %d",revdest[i][j]);
}
*/
return;
}
Compilation message
garden.cpp: In function 'void dfs1(int, int)':
garden.cpp:22:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int x=0;x<revdest[i].size();x++)
~^~~~~~~~~~~~~~~~~~
garden.cpp: In function 'void dfs2(int, int)':
garden.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int x=0;x<revdest[i].size();x++)
~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11132 KB |
Output is correct |
2 |
Correct |
12 ms |
11064 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
11000 KB |
Output is correct |
5 |
Correct |
11 ms |
11004 KB |
Output is correct |
6 |
Correct |
13 ms |
11236 KB |
Output is correct |
7 |
Correct |
12 ms |
11008 KB |
Output is correct |
8 |
Correct |
12 ms |
11128 KB |
Output is correct |
9 |
Correct |
15 ms |
11284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11132 KB |
Output is correct |
2 |
Correct |
12 ms |
11064 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
11000 KB |
Output is correct |
5 |
Correct |
11 ms |
11004 KB |
Output is correct |
6 |
Correct |
13 ms |
11236 KB |
Output is correct |
7 |
Correct |
12 ms |
11008 KB |
Output is correct |
8 |
Correct |
12 ms |
11128 KB |
Output is correct |
9 |
Correct |
15 ms |
11284 KB |
Output is correct |
10 |
Correct |
13 ms |
11004 KB |
Output is correct |
11 |
Correct |
26 ms |
13316 KB |
Output is correct |
12 |
Correct |
53 ms |
15084 KB |
Output is correct |
13 |
Correct |
66 ms |
27828 KB |
Output is correct |
14 |
Correct |
162 ms |
24860 KB |
Output is correct |
15 |
Correct |
309 ms |
25748 KB |
Output is correct |
16 |
Correct |
169 ms |
21176 KB |
Output is correct |
17 |
Correct |
137 ms |
19740 KB |
Output is correct |
18 |
Correct |
66 ms |
14732 KB |
Output is correct |
19 |
Correct |
216 ms |
24704 KB |
Output is correct |
20 |
Correct |
245 ms |
25036 KB |
Output is correct |
21 |
Correct |
159 ms |
20240 KB |
Output is correct |
22 |
Correct |
141 ms |
19788 KB |
Output is correct |
23 |
Correct |
168 ms |
24712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11132 KB |
Output is correct |
2 |
Correct |
12 ms |
11064 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
11000 KB |
Output is correct |
5 |
Correct |
11 ms |
11004 KB |
Output is correct |
6 |
Correct |
13 ms |
11236 KB |
Output is correct |
7 |
Correct |
12 ms |
11008 KB |
Output is correct |
8 |
Correct |
12 ms |
11128 KB |
Output is correct |
9 |
Correct |
15 ms |
11284 KB |
Output is correct |
10 |
Correct |
13 ms |
11004 KB |
Output is correct |
11 |
Correct |
26 ms |
13316 KB |
Output is correct |
12 |
Correct |
53 ms |
15084 KB |
Output is correct |
13 |
Correct |
66 ms |
27828 KB |
Output is correct |
14 |
Correct |
162 ms |
24860 KB |
Output is correct |
15 |
Correct |
309 ms |
25748 KB |
Output is correct |
16 |
Correct |
169 ms |
21176 KB |
Output is correct |
17 |
Correct |
137 ms |
19740 KB |
Output is correct |
18 |
Correct |
66 ms |
14732 KB |
Output is correct |
19 |
Correct |
216 ms |
24704 KB |
Output is correct |
20 |
Correct |
245 ms |
25036 KB |
Output is correct |
21 |
Correct |
159 ms |
20240 KB |
Output is correct |
22 |
Correct |
141 ms |
19788 KB |
Output is correct |
23 |
Correct |
168 ms |
24712 KB |
Output is correct |
24 |
Correct |
13 ms |
11052 KB |
Output is correct |
25 |
Correct |
134 ms |
13212 KB |
Output is correct |
26 |
Correct |
183 ms |
14804 KB |
Output is correct |
27 |
Correct |
1654 ms |
27360 KB |
Output is correct |
28 |
Correct |
1539 ms |
24784 KB |
Output is correct |
29 |
Correct |
2143 ms |
25048 KB |
Output is correct |
30 |
Correct |
1218 ms |
20680 KB |
Output is correct |
31 |
Correct |
1115 ms |
19860 KB |
Output is correct |
32 |
Correct |
207 ms |
14752 KB |
Output is correct |
33 |
Correct |
1537 ms |
24672 KB |
Output is correct |
34 |
Correct |
2065 ms |
25152 KB |
Output is correct |
35 |
Correct |
1256 ms |
20672 KB |
Output is correct |
36 |
Correct |
1565 ms |
19824 KB |
Output is correct |
37 |
Correct |
1145 ms |
24836 KB |
Output is correct |
38 |
Correct |
2866 ms |
35096 KB |
Output is correct |