#include "garden.h"
#include "gardenlib.h"
#include <stdbool.h>
#include <assert.h>
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
static int most_beautiful[150001][2];
static int graph[300001];
static int rem[2][300001];
int cyc[2] = { 1e9 + 1, 1e9 + 1 };
static int stack[300000];
int sz = 0;
// 1 indexing, graph[u] == 0, means no edges connected to it
P++;
// save 2 most_beautiful edges
for (int i = 0; i < M; i++) {
for (int j = 0; j < 2; j++) {
int u = R[i][j] + 1;
int v = R[i][j^1] + 1;
if (most_beautiful[u][0] == 0) {
most_beautiful[u][0] = v;
} else if (most_beautiful[u][1] == 0) {
most_beautiful[u][1] = v;
}
}
}
// build functional graph
for (int u = 1; u <= N; u++) {
for (int j = 0; j < 2; j++) {
int v = most_beautiful[u][j];
if (most_beautiful[v][0] != u || most_beautiful[v][1] == 0)
graph[u + j*N] = v;
else
graph[u + j*N] = v + N;
}
}
for (int u = 1; u <= N; u++) {
if (graph[u] == 0) continue;
if (rem[0][u] != 0 || u == P) {
assert(rem[1][u] != 0 || u == P || u == P + N);
continue;
}
int idx[2] = { -1, -1 };
int v = u;
while (rem[0][v] == 0 && rem[1][v] == 0) {
rem[0][v] = rem[1][v] = -1;
if (v == P) idx[0] = sz;
else if (v == P + N) idx[1] = sz;
stack[sz++] = v;
v = graph[v];
}
int cyc_idx = -1;
while (++cyc_idx < sz && stack[cyc_idx] != v);
if (cyc_idx < sz) {
// cycle found
int c = sz - cyc_idx;
for (int i = 0; i < 2; i++) {
if (idx[i] == -1) continue;
if (idx[i] >= cyc_idx) {
// part of cycle
cyc[i] = c;
for (int j = 1; j + idx[i] < sz; j++)
rem[i][ stack[j + idx[i]] ] = c - j;
}
}
} else {
// no cycle
for (int i = 0; i < 2; i++) {
if (rem[i][v] != -1)
for (int j = idx[i] + 1; j < sz; j++)
rem[i][ stack[j] ] = sz - j + rem[i][v];
}
}
for (int i = 0; i < 2; i++)
for (int j = 0; j <= idx[i]; j++)
rem[i][ stack[j] ] = idx[i] - j;
sz = 0;
}
for (int i = 0; i < Q; i++) {
int res = 0;
for (int j = 0; j < 2; j++) {
for (int k = 1; k <= N; k++) {
if (rem[j][k] == -1) continue;
res += rem[j][k] <= G[i] && (G[i] - rem[j][k]) % cyc[j] == 0;
}
}
answer(res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
1372 KB |
Output is correct |
12 |
Correct |
8 ms |
2392 KB |
Output is correct |
13 |
Correct |
11 ms |
5248 KB |
Output is correct |
14 |
Correct |
20 ms |
6320 KB |
Output is correct |
15 |
Correct |
29 ms |
6324 KB |
Output is correct |
16 |
Correct |
18 ms |
5036 KB |
Output is correct |
17 |
Correct |
25 ms |
4808 KB |
Output is correct |
18 |
Correct |
10 ms |
2392 KB |
Output is correct |
19 |
Correct |
19 ms |
7252 KB |
Output is correct |
20 |
Correct |
20 ms |
7604 KB |
Output is correct |
21 |
Correct |
22 ms |
5880 KB |
Output is correct |
22 |
Correct |
20 ms |
5376 KB |
Output is correct |
23 |
Correct |
18 ms |
6844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
1372 KB |
Output is correct |
12 |
Correct |
8 ms |
2392 KB |
Output is correct |
13 |
Correct |
11 ms |
5248 KB |
Output is correct |
14 |
Correct |
20 ms |
6320 KB |
Output is correct |
15 |
Correct |
29 ms |
6324 KB |
Output is correct |
16 |
Correct |
18 ms |
5036 KB |
Output is correct |
17 |
Correct |
25 ms |
4808 KB |
Output is correct |
18 |
Correct |
10 ms |
2392 KB |
Output is correct |
19 |
Correct |
19 ms |
7252 KB |
Output is correct |
20 |
Correct |
20 ms |
7604 KB |
Output is correct |
21 |
Correct |
22 ms |
5880 KB |
Output is correct |
22 |
Correct |
20 ms |
5376 KB |
Output is correct |
23 |
Correct |
18 ms |
6844 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
62 ms |
1532 KB |
Output is correct |
26 |
Correct |
99 ms |
2400 KB |
Output is correct |
27 |
Correct |
572 ms |
5208 KB |
Output is correct |
28 |
Correct |
589 ms |
6412 KB |
Output is correct |
29 |
Correct |
725 ms |
6568 KB |
Output is correct |
30 |
Correct |
468 ms |
5208 KB |
Output is correct |
31 |
Correct |
402 ms |
4692 KB |
Output is correct |
32 |
Correct |
107 ms |
2392 KB |
Output is correct |
33 |
Correct |
663 ms |
7476 KB |
Output is correct |
34 |
Correct |
735 ms |
7632 KB |
Output is correct |
35 |
Correct |
511 ms |
5900 KB |
Output is correct |
36 |
Correct |
796 ms |
5468 KB |
Output is correct |
37 |
Correct |
643 ms |
6992 KB |
Output is correct |
38 |
Correct |
1876 ms |
8740 KB |
Output is correct |