/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
#include "garden.h"
using namespace std;
typedef long long ll;
void answer (int x);
void count_routes (int N, int M, int P, int R[][2], int Q, int G[])
{
vector <vector <int>> adj (N);
for(int i = 0; i < M; i++)
{
adj[R[i][0]].push_back(R[i][1]);
adj[R[i][1]].push_back(R[i][0]);
}
vector <int> edge (N * 2);
for(int u = 0; u < N; u++)
{
edge[u] = adj[u][0];
if(adj[edge[u]][0] == u)
edge[u] += N;
}
for(int u = 0; u < N; u++)
{
if((int) adj[u].size() >= 2)
edge[N + u] = adj[u][1];
else
edge[N + u] = adj[u][0];
if(adj[edge[N + u]][0] == u)
edge[N + u] += N;
}
vector <vector <int>> rev (N * 2);
for(int u = 0; u < N * 2; u++)
rev[edge[u]].push_back(u);
function <void (int, vector <int> &, int&)> bfs = [&] (int start, vector <int> &dist, int &cycle)
{
dist = vector <int> (N * 2, INT_MAX);
queue <int> q;
q.push(start);
dist[start] = 0;
while(q.empty() == false)
{
int u = q.front();
q.pop();
for(int v : rev[u])
if(dist[v] == INT_MAX)
{
dist[v] = dist[u] + 1;
q.push(v);
}
}
cycle = 0;
vector <bool> seen (N * 2, false);
int u = start;
while(seen[u] == false)
{
cycle++;
seen[u] = true;
u = edge[u];
}
if(u != start || cycle == 1)
cycle = INT_MAX;
};
vector <int> distA, distB;
int cycleA, cycleB;
bfs(P, distA, cycleA);
bfs(N + P, distB, cycleB);
for(int i = 0; i < Q; i++)
{
int K = G[i];
int cnt = 0;
for(int i = 0; i < N; i++)
{
if(distA[i] <= K && (distA[i] - K) % cycleA == 0)
cnt++;
if(distB[i] <= K && (distB[i] - K) % cycleB == 0)
cnt++;
}
answer(cnt);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
660 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
13 ms |
4448 KB |
Output is correct |
12 |
Correct |
29 ms |
7372 KB |
Output is correct |
13 |
Correct |
60 ms |
17968 KB |
Output is correct |
14 |
Correct |
135 ms |
24812 KB |
Output is correct |
15 |
Correct |
137 ms |
25140 KB |
Output is correct |
16 |
Correct |
121 ms |
17924 KB |
Output is correct |
17 |
Correct |
87 ms |
15828 KB |
Output is correct |
18 |
Correct |
29 ms |
7388 KB |
Output is correct |
19 |
Correct |
110 ms |
24664 KB |
Output is correct |
20 |
Correct |
150 ms |
25264 KB |
Output is correct |
21 |
Correct |
97 ms |
17972 KB |
Output is correct |
22 |
Correct |
87 ms |
15812 KB |
Output is correct |
23 |
Correct |
134 ms |
27256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
660 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
13 ms |
4448 KB |
Output is correct |
12 |
Correct |
29 ms |
7372 KB |
Output is correct |
13 |
Correct |
60 ms |
17968 KB |
Output is correct |
14 |
Correct |
135 ms |
24812 KB |
Output is correct |
15 |
Correct |
137 ms |
25140 KB |
Output is correct |
16 |
Correct |
121 ms |
17924 KB |
Output is correct |
17 |
Correct |
87 ms |
15828 KB |
Output is correct |
18 |
Correct |
29 ms |
7388 KB |
Output is correct |
19 |
Correct |
110 ms |
24664 KB |
Output is correct |
20 |
Correct |
150 ms |
25264 KB |
Output is correct |
21 |
Correct |
97 ms |
17972 KB |
Output is correct |
22 |
Correct |
87 ms |
15812 KB |
Output is correct |
23 |
Correct |
134 ms |
27256 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
110 ms |
4532 KB |
Output is correct |
26 |
Correct |
158 ms |
7432 KB |
Output is correct |
27 |
Correct |
2566 ms |
18024 KB |
Output is correct |
28 |
Correct |
1107 ms |
24808 KB |
Output is correct |
29 |
Correct |
2826 ms |
25272 KB |
Output is correct |
30 |
Correct |
1672 ms |
18084 KB |
Output is correct |
31 |
Correct |
1665 ms |
15892 KB |
Output is correct |
32 |
Correct |
171 ms |
7492 KB |
Output is correct |
33 |
Correct |
1116 ms |
24792 KB |
Output is correct |
34 |
Correct |
2856 ms |
25252 KB |
Output is correct |
35 |
Correct |
1800 ms |
18112 KB |
Output is correct |
36 |
Correct |
1937 ms |
16020 KB |
Output is correct |
37 |
Correct |
874 ms |
27316 KB |
Output is correct |
38 |
Correct |
2325 ms |
31576 KB |
Output is correct |