#pragma GCC optimize("Ofast")
#include "garden.h"
#include "gardenlib.h"
#include <vector>
#include <iostream>
using namespace std;
struct graph
{
int n;
int h;
vector<vector<int>> adjlist;
vector<vector<int>> best;
vector<int> dist;
vector<bool> cycle;
int cyc = 2e9;
int p;
void add(int fr, int to)
{
int t = to;
if (fr == best[to][0]) t += h;
if (best[fr][0] == to)
{
adjlist[fr].push_back(t);
}
if (best[fr][1] == to)
{
adjlist[fr + h].push_back(t);
}
}
graph(int in, int m, vector<vector<int>> ib, int R[][2]) : n(2*in), h(in), adjlist(vector<vector<int>>(2*n)), best(ib), dist(n, -1), cycle(n)
{
for (int i = 0; i < m; i++)
{
add(R[i][0], R[i][1]);
add(R[i][1], R[i][0]);
}
}
void print()
{
cout << n << "\n";
for (int i = 0; i < n; i++)
{
for (auto j : adjlist[i])
{
cout << i << " " << j << "\n";
}
}
}
void reverse()
{
vector<vector<int>> old(n);
swap(old, adjlist);
for (int i = 0; i < n; i++)
{
for (int j : old[i])
{
adjlist[j].push_back(i);
}
}
}
bool dfs(int curr, int d)
{
if (curr == p && dist[curr] != -1)
{
cyc = d;
return true;
}
dist[curr] = d;
for (auto next : adjlist[curr])
{
if (dfs(next, d + 1)) cycle[curr] = true;
}
return cycle[curr];
}
void calc(int P)
{
p = P;
dfs(p, 0);
}
void pcyc()
{
cout << "Cycle: " << cyc << "\n";
for (int i = 0; i < n; i++)
{
cout << (i % h);
if (i >= h) cout << "*";
else cout << " ";
cout << ": ";
cout << cycle[i] << " " << dist[i] << "\n";
}
}
bool check(int v, int t)
{
if (t < dist[v]) return false;
if (t % cyc != dist[v] % cyc) return false;
return true;
}
};
void count_routes(int n, int m, int P, int R[][2], int Q, int G[])
{
vector<vector<int>> best(n, vector<int>(2, -1));
auto ins = [&](int k, int v)
{
swap(best[k][0], best[k][1]);
best[k][0] = v;
};
for (int i = m - 1; i >= 0; i--)
{
ins(R[i][0], R[i][1]);
ins(R[i][1], R[i][0]);
}
for (int i = 0; i < n; i++) if (best[i][1] == -1) best[i][1] = best[i][0];
graph g(n, m, best, R);
g.reverse();
graph g2(g);
g.calc(P);
g2.calc(P + n);
for (int c = 0; c < Q; c++)
{
int v = G[c];
int r = 0;
for (int i = 0; i < n; i++)
{
r += g.check(i, v);
r += g2.check(i, v);
}
answer(r);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
33 ms |
8688 KB |
Output is correct |
12 |
Correct |
64 ms |
14224 KB |
Output is correct |
13 |
Correct |
112 ms |
43124 KB |
Output is correct |
14 |
Correct |
212 ms |
49404 KB |
Output is correct |
15 |
Correct |
238 ms |
50036 KB |
Output is correct |
16 |
Correct |
171 ms |
34324 KB |
Output is correct |
17 |
Correct |
147 ms |
28916 KB |
Output is correct |
18 |
Correct |
54 ms |
14216 KB |
Output is correct |
19 |
Correct |
203 ms |
49420 KB |
Output is correct |
20 |
Correct |
242 ms |
50012 KB |
Output is correct |
21 |
Correct |
168 ms |
34196 KB |
Output is correct |
22 |
Correct |
164 ms |
28916 KB |
Output is correct |
23 |
Correct |
225 ms |
54820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
33 ms |
8688 KB |
Output is correct |
12 |
Correct |
64 ms |
14224 KB |
Output is correct |
13 |
Correct |
112 ms |
43124 KB |
Output is correct |
14 |
Correct |
212 ms |
49404 KB |
Output is correct |
15 |
Correct |
238 ms |
50036 KB |
Output is correct |
16 |
Correct |
171 ms |
34324 KB |
Output is correct |
17 |
Correct |
147 ms |
28916 KB |
Output is correct |
18 |
Correct |
54 ms |
14216 KB |
Output is correct |
19 |
Correct |
203 ms |
49420 KB |
Output is correct |
20 |
Correct |
242 ms |
50012 KB |
Output is correct |
21 |
Correct |
168 ms |
34196 KB |
Output is correct |
22 |
Correct |
164 ms |
28916 KB |
Output is correct |
23 |
Correct |
225 ms |
54820 KB |
Output is correct |
24 |
Correct |
10 ms |
384 KB |
Output is correct |
25 |
Correct |
1103 ms |
8824 KB |
Output is correct |
26 |
Correct |
1988 ms |
14216 KB |
Output is correct |
27 |
Correct |
4357 ms |
43108 KB |
Output is correct |
28 |
Execution timed out |
5050 ms |
49428 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |