#include "garden.h"
#include "gardenlib.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];
// 10^9 < 2^30
static int nxt[30][300001];
// 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;
}
}
// binary lift
for (int u = 1; u <= 2 * N; u++)
nxt[0][u] = graph[u];
for (int p = 1; p < 30; p++)
for (int u = 1; u <= 2 * N; u++)
nxt[p][u] = nxt[p-1][ nxt[p-1][u] ];
for (int i = 0; i < Q; i++) {
int res = 0;
for (int j = 1; j <= N; j++) {
int u = j;
for (int p = 0; p < 30; p++)
if (G[i] >> p & 1)
u = nxt[p][u];
res += u == P || u == P + N;
}
answer(res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
6624 KB |
Output is correct |
12 |
Correct |
13 ms |
10796 KB |
Output is correct |
13 |
Correct |
25 ms |
23396 KB |
Output is correct |
14 |
Correct |
42 ms |
36948 KB |
Output is correct |
15 |
Correct |
50 ms |
37456 KB |
Output is correct |
16 |
Correct |
31 ms |
25944 KB |
Output is correct |
17 |
Correct |
39 ms |
22240 KB |
Output is correct |
18 |
Correct |
16 ms |
10840 KB |
Output is correct |
19 |
Correct |
62 ms |
37068 KB |
Output is correct |
20 |
Correct |
46 ms |
37464 KB |
Output is correct |
21 |
Correct |
32 ms |
25940 KB |
Output is correct |
22 |
Correct |
29 ms |
22152 KB |
Output is correct |
23 |
Correct |
44 ms |
40796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
6624 KB |
Output is correct |
12 |
Correct |
13 ms |
10796 KB |
Output is correct |
13 |
Correct |
25 ms |
23396 KB |
Output is correct |
14 |
Correct |
42 ms |
36948 KB |
Output is correct |
15 |
Correct |
50 ms |
37456 KB |
Output is correct |
16 |
Correct |
31 ms |
25944 KB |
Output is correct |
17 |
Correct |
39 ms |
22240 KB |
Output is correct |
18 |
Correct |
16 ms |
10840 KB |
Output is correct |
19 |
Correct |
62 ms |
37068 KB |
Output is correct |
20 |
Correct |
46 ms |
37464 KB |
Output is correct |
21 |
Correct |
32 ms |
25940 KB |
Output is correct |
22 |
Correct |
29 ms |
22152 KB |
Output is correct |
23 |
Correct |
44 ms |
40796 KB |
Output is correct |
24 |
Correct |
10 ms |
600 KB |
Output is correct |
25 |
Correct |
1368 ms |
6492 KB |
Output is correct |
26 |
Correct |
2219 ms |
11004 KB |
Output is correct |
27 |
Correct |
3723 ms |
23588 KB |
Output is correct |
28 |
Execution timed out |
5056 ms |
37180 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |