#include "garden.h"
#include "gardenlib.h"
#include <iostream>
#include<vector>
using namespace std;
///Duplicate nodes
///Directed graph
///Determine the number of nodes that can follow a path and end in P in exactly K steps
vector<int> adjList[300005];
vector<int> original[150005];
short vis[300005];
void buildGraph(int N) ///Creates duplicated and reversed graph
{
///Real node uses its prncipal edge
///Duplicated node uses its secondary edge
for(int u=0;u<N;u++){
int v=original[u][0]; //Principal edge
if(original[v][0]!=u || original[v].size()==1){
adjList[v].push_back(u);
}
else{
adjList[v+N].push_back(u);
}
if(original[u].size()==1) continue;
v=original[u][1]; //Secondary edge
if(original[v][0]!=u || original[v].size()==1){
adjList[v].push_back(u+N);
}
else{
adjList[v+N].push_back(u+N);
}
}
}
int color[300005];
int d[300005][2];
int C[2]={-1,-1}; //Cycle Size
int inCycle[2];
int t=0;
void dfs(int u) ///Computes distance from P and P+N (duplicated node) to all nodes
{
color[u]=1;
for(int v: adjList[u]){
if(color[v]==0){
d[v][t]=d[u][t]+1;
dfs(v);
}
else if(color[v]==1){ //P/P+N is on a cycle
C[t]=d[u][t]+1;
inCycle[t]=1;
}
}
color[u]=-1;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
for(int i=0;i<M;i++){
int u=R[i][0];
int v=R[i][1];
original[u].push_back(v);
original[v].push_back(u);
}
buildGraph(N);
for(int i=0;i<2*N;i++){
d[i][0]=d[i][1]=1000000000;
}
d[P][0]=0;
dfs(P);
t++;
for(int i=0;i<2*N;i++){
color[i]=0;
}
d[P+N][1]=0;
dfs(P+N);
for(int i=0;i<Q;i++){ //For each query
int k=G[i];
int total=0;
for(int u=0;u<N;u++){ //For each node
for(int j=0;j<2;j++){ //Checks if the distance is 0 modulo C
if(d[u][j]==1000000000) continue; //Not reachable
if(inCycle[j]){ //Cycle
int D=k-d[u][j];
if(D>=0){
if(D%C[j]==0) total++;
}
}
else{
int D=k-d[u][j]; //Distance is exactly the same
if(D==0) total++;
}
}
}
answer(total);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11100 KB |
Output is correct |
2 |
Correct |
12 ms |
11036 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
10920 KB |
Output is correct |
5 |
Correct |
11 ms |
10872 KB |
Output is correct |
6 |
Correct |
12 ms |
11128 KB |
Output is correct |
7 |
Correct |
11 ms |
11000 KB |
Output is correct |
8 |
Correct |
12 ms |
10996 KB |
Output is correct |
9 |
Correct |
15 ms |
11140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11100 KB |
Output is correct |
2 |
Correct |
12 ms |
11036 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
10920 KB |
Output is correct |
5 |
Correct |
11 ms |
10872 KB |
Output is correct |
6 |
Correct |
12 ms |
11128 KB |
Output is correct |
7 |
Correct |
11 ms |
11000 KB |
Output is correct |
8 |
Correct |
12 ms |
10996 KB |
Output is correct |
9 |
Correct |
15 ms |
11140 KB |
Output is correct |
10 |
Correct |
11 ms |
10872 KB |
Output is correct |
11 |
Correct |
24 ms |
13304 KB |
Output is correct |
12 |
Correct |
49 ms |
14864 KB |
Output is correct |
13 |
Correct |
70 ms |
29624 KB |
Output is correct |
14 |
Correct |
153 ms |
24016 KB |
Output is correct |
15 |
Correct |
196 ms |
24376 KB |
Output is correct |
16 |
Correct |
152 ms |
20708 KB |
Output is correct |
17 |
Correct |
141 ms |
19484 KB |
Output is correct |
18 |
Correct |
48 ms |
14836 KB |
Output is correct |
19 |
Correct |
163 ms |
24044 KB |
Output is correct |
20 |
Correct |
198 ms |
24296 KB |
Output is correct |
21 |
Correct |
164 ms |
20836 KB |
Output is correct |
22 |
Correct |
140 ms |
19472 KB |
Output is correct |
23 |
Correct |
164 ms |
25304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11100 KB |
Output is correct |
2 |
Correct |
12 ms |
11036 KB |
Output is correct |
3 |
Correct |
12 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
10920 KB |
Output is correct |
5 |
Correct |
11 ms |
10872 KB |
Output is correct |
6 |
Correct |
12 ms |
11128 KB |
Output is correct |
7 |
Correct |
11 ms |
11000 KB |
Output is correct |
8 |
Correct |
12 ms |
10996 KB |
Output is correct |
9 |
Correct |
15 ms |
11140 KB |
Output is correct |
10 |
Correct |
11 ms |
10872 KB |
Output is correct |
11 |
Correct |
24 ms |
13304 KB |
Output is correct |
12 |
Correct |
49 ms |
14864 KB |
Output is correct |
13 |
Correct |
70 ms |
29624 KB |
Output is correct |
14 |
Correct |
153 ms |
24016 KB |
Output is correct |
15 |
Correct |
196 ms |
24376 KB |
Output is correct |
16 |
Correct |
152 ms |
20708 KB |
Output is correct |
17 |
Correct |
141 ms |
19484 KB |
Output is correct |
18 |
Correct |
48 ms |
14836 KB |
Output is correct |
19 |
Correct |
163 ms |
24044 KB |
Output is correct |
20 |
Correct |
198 ms |
24296 KB |
Output is correct |
21 |
Correct |
164 ms |
20836 KB |
Output is correct |
22 |
Correct |
140 ms |
19472 KB |
Output is correct |
23 |
Correct |
164 ms |
25304 KB |
Output is correct |
24 |
Correct |
13 ms |
10872 KB |
Output is correct |
25 |
Correct |
159 ms |
13372 KB |
Output is correct |
26 |
Correct |
227 ms |
14892 KB |
Output is correct |
27 |
Correct |
1652 ms |
29632 KB |
Output is correct |
28 |
Correct |
1648 ms |
24060 KB |
Output is correct |
29 |
Correct |
1793 ms |
24312 KB |
Output is correct |
30 |
Correct |
1146 ms |
20784 KB |
Output is correct |
31 |
Correct |
1108 ms |
19476 KB |
Output is correct |
32 |
Correct |
277 ms |
14892 KB |
Output is correct |
33 |
Correct |
1676 ms |
24052 KB |
Output is correct |
34 |
Correct |
1712 ms |
24300 KB |
Output is correct |
35 |
Correct |
1131 ms |
20676 KB |
Output is correct |
36 |
Correct |
1694 ms |
19480 KB |
Output is correct |
37 |
Correct |
1397 ms |
25364 KB |
Output is correct |
38 |
Correct |
2896 ms |
36792 KB |
Output is correct |