#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 + c * (v >= N);
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 <= 2 * 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);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
516 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
1720 KB |
Output is correct |
12 |
Correct |
8 ms |
7440 KB |
Output is correct |
13 |
Correct |
14 ms |
8024 KB |
Output is correct |
14 |
Correct |
26 ms |
8284 KB |
Output is correct |
15 |
Correct |
25 ms |
9308 KB |
Output is correct |
16 |
Correct |
24 ms |
8796 KB |
Output is correct |
17 |
Correct |
22 ms |
5880 KB |
Output is correct |
18 |
Correct |
9 ms |
2904 KB |
Output is correct |
19 |
Correct |
27 ms |
8252 KB |
Output is correct |
20 |
Correct |
28 ms |
8528 KB |
Output is correct |
21 |
Correct |
26 ms |
6524 KB |
Output is correct |
22 |
Correct |
24 ms |
8628 KB |
Output is correct |
23 |
Correct |
27 ms |
9068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
516 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
1720 KB |
Output is correct |
12 |
Correct |
8 ms |
7440 KB |
Output is correct |
13 |
Correct |
14 ms |
8024 KB |
Output is correct |
14 |
Correct |
26 ms |
8284 KB |
Output is correct |
15 |
Correct |
25 ms |
9308 KB |
Output is correct |
16 |
Correct |
24 ms |
8796 KB |
Output is correct |
17 |
Correct |
22 ms |
5880 KB |
Output is correct |
18 |
Correct |
9 ms |
2904 KB |
Output is correct |
19 |
Correct |
27 ms |
8252 KB |
Output is correct |
20 |
Correct |
28 ms |
8528 KB |
Output is correct |
21 |
Correct |
26 ms |
6524 KB |
Output is correct |
22 |
Correct |
24 ms |
8628 KB |
Output is correct |
23 |
Correct |
27 ms |
9068 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
99 ms |
1756 KB |
Output is correct |
26 |
Correct |
167 ms |
2912 KB |
Output is correct |
27 |
Correct |
632 ms |
8024 KB |
Output is correct |
28 |
Correct |
829 ms |
9304 KB |
Output is correct |
29 |
Correct |
897 ms |
9392 KB |
Output is correct |
30 |
Correct |
501 ms |
8796 KB |
Output is correct |
31 |
Correct |
492 ms |
8540 KB |
Output is correct |
32 |
Correct |
167 ms |
7256 KB |
Output is correct |
33 |
Correct |
807 ms |
8520 KB |
Output is correct |
34 |
Correct |
843 ms |
8536 KB |
Output is correct |
35 |
Correct |
581 ms |
8796 KB |
Output is correct |
36 |
Correct |
846 ms |
6044 KB |
Output is correct |
37 |
Correct |
727 ms |
9492 KB |
Output is correct |
38 |
Correct |
1872 ms |
9552 KB |
Output is correct |