#include "garden.h"
#include "gardenlib.h"
#include <stdbool.h>
#include <string.h>
#include <assert.h>
int dsu_find(int *par, int a) {
return par[a] < 0 ? a : (par[a] = dsu_find(par, par[a]));
}
void dsu_union(int *par, int a, int b) {
a = dsu_find(par, a);
b = dsu_find(par, b);
assert(a != b); // we should only call dsu_union if they belong to different components
if (-par[b] > -par[a]) {
int tmp = a;
a = b;
b = tmp;
}
par[a] += par[b];
par[b] = a;
}
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 dsu[300001];
static int rem[2][300001];
int cyc[2];
cyc[0] = cyc[1] = 1e9 + 1;
memset(dsu, -1, sizeof dsu[0] * (2 * (size_t)N + 1));
assert(dsu[2 * N] == -1);
// 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;
}
}
// set everything in cycle
for (int u = 1; u <= 2 * N; u++) {
if (graph[u] == 0) continue;
if (dsu_find(dsu, u) != dsu_find(dsu, graph[u])) {
dsu_union(dsu, u, graph[u]);
continue;
}
bool seen[2] = {false};
int c = 0;
int v = u;
do {
v = graph[v];
seen[0] = seen[0] || v == P;
seen[1] = seen[1] || v == P + N;
rem[0][v] = rem[1][v] = -1;
c++;
} while (v != u);
for (int i = 0; i < 2; i++) {
if (!seen[i]) continue;
cyc[i] = c;
v = graph[P + i * N];
for (int j = 1; j <= c; j++) {
rem[i][v] = c - j;
v = graph[v];
}
assert(v == graph[P + i * N]);
}
}
// set everything out of cycle
for (int i = 0; i < 2; i++) {
int target = P + i * N;
for (int u = 1; u <= N; u++) {
if (rem[i][u] != 0 || u == target) continue;
int v = u;
int d = 0;
do {
rem[i][v] = -1;
d++;
v = graph[v];
} while (rem[i][v] == 0 && v != target);
if (v == target) {
assert(rem[i][target] == -1 || rem[i][target] == 0);
for (v = u; v != graph[target]; v = graph[v])
rem[i][v] = d--;
assert(d == -1);
assert(rem[i][target] == 0);
} else if (rem[i][v] > 0) {
for (int w = u; w != v; w = graph[w])
rem[i][w] = rem[i][v] + d--;
assert(d == 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
1464 KB |
Output is correct |
12 |
Correct |
8 ms |
2652 KB |
Output is correct |
13 |
Correct |
13 ms |
5340 KB |
Output is correct |
14 |
Correct |
24 ms |
7244 KB |
Output is correct |
15 |
Correct |
25 ms |
7576 KB |
Output is correct |
16 |
Correct |
22 ms |
5724 KB |
Output is correct |
17 |
Correct |
21 ms |
5276 KB |
Output is correct |
18 |
Correct |
9 ms |
2908 KB |
Output is correct |
19 |
Correct |
26 ms |
8400 KB |
Output is correct |
20 |
Correct |
25 ms |
8596 KB |
Output is correct |
21 |
Correct |
22 ms |
6492 KB |
Output is correct |
22 |
Correct |
23 ms |
5980 KB |
Output is correct |
23 |
Correct |
24 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
1464 KB |
Output is correct |
12 |
Correct |
8 ms |
2652 KB |
Output is correct |
13 |
Correct |
13 ms |
5340 KB |
Output is correct |
14 |
Correct |
24 ms |
7244 KB |
Output is correct |
15 |
Correct |
25 ms |
7576 KB |
Output is correct |
16 |
Correct |
22 ms |
5724 KB |
Output is correct |
17 |
Correct |
21 ms |
5276 KB |
Output is correct |
18 |
Correct |
9 ms |
2908 KB |
Output is correct |
19 |
Correct |
26 ms |
8400 KB |
Output is correct |
20 |
Correct |
25 ms |
8596 KB |
Output is correct |
21 |
Correct |
22 ms |
6492 KB |
Output is correct |
22 |
Correct |
23 ms |
5980 KB |
Output is correct |
23 |
Correct |
24 ms |
8020 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
70 ms |
1692 KB |
Output is correct |
26 |
Correct |
101 ms |
2652 KB |
Output is correct |
27 |
Correct |
628 ms |
5212 KB |
Output is correct |
28 |
Correct |
640 ms |
7324 KB |
Output is correct |
29 |
Correct |
776 ms |
7512 KB |
Output is correct |
30 |
Correct |
455 ms |
5972 KB |
Output is correct |
31 |
Correct |
415 ms |
5512 KB |
Output is correct |
32 |
Correct |
126 ms |
2908 KB |
Output is correct |
33 |
Correct |
624 ms |
8280 KB |
Output is correct |
34 |
Correct |
692 ms |
8532 KB |
Output is correct |
35 |
Correct |
489 ms |
6488 KB |
Output is correct |
36 |
Correct |
751 ms |
5980 KB |
Output is correct |
37 |
Correct |
531 ms |
8020 KB |
Output is correct |
38 |
Correct |
1780 ms |
9320 KB |
Output is correct |