#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 |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 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 |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 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 |
27 ms |
8688 KB |
Output is correct |
12 |
Correct |
54 ms |
14240 KB |
Output is correct |
13 |
Correct |
97 ms |
43124 KB |
Output is correct |
14 |
Correct |
205 ms |
49432 KB |
Output is correct |
15 |
Correct |
239 ms |
50040 KB |
Output is correct |
16 |
Correct |
165 ms |
34196 KB |
Output is correct |
17 |
Correct |
185 ms |
28900 KB |
Output is correct |
18 |
Correct |
53 ms |
14228 KB |
Output is correct |
19 |
Correct |
202 ms |
49428 KB |
Output is correct |
20 |
Correct |
244 ms |
50040 KB |
Output is correct |
21 |
Correct |
184 ms |
34196 KB |
Output is correct |
22 |
Correct |
161 ms |
28916 KB |
Output is correct |
23 |
Correct |
232 ms |
54712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 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 |
27 ms |
8688 KB |
Output is correct |
12 |
Correct |
54 ms |
14240 KB |
Output is correct |
13 |
Correct |
97 ms |
43124 KB |
Output is correct |
14 |
Correct |
205 ms |
49432 KB |
Output is correct |
15 |
Correct |
239 ms |
50040 KB |
Output is correct |
16 |
Correct |
165 ms |
34196 KB |
Output is correct |
17 |
Correct |
185 ms |
28900 KB |
Output is correct |
18 |
Correct |
53 ms |
14228 KB |
Output is correct |
19 |
Correct |
202 ms |
49428 KB |
Output is correct |
20 |
Correct |
244 ms |
50040 KB |
Output is correct |
21 |
Correct |
184 ms |
34196 KB |
Output is correct |
22 |
Correct |
161 ms |
28916 KB |
Output is correct |
23 |
Correct |
232 ms |
54712 KB |
Output is correct |
24 |
Correct |
10 ms |
384 KB |
Output is correct |
25 |
Correct |
1103 ms |
8716 KB |
Output is correct |
26 |
Correct |
1987 ms |
14088 KB |
Output is correct |
27 |
Correct |
4369 ms |
43364 KB |
Output is correct |
28 |
Execution timed out |
5052 ms |
49432 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |