#include "garden.h"
#include "gardenlib.h"
#include <stdlib.h>
#define N 150000
#define M 150000
#define Q 2000
#define L 29 /* L = floor(log2(10^9)) */
#define INF 0x3f3f3f3f
void append(int **eh, int *eo, int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int *eh[N], eo[N], *fj[M * 2], fo[M * 2];
int qu1[M * 2], cnt1, qu2[M * 2], cnt2; char src[M * 2], dest[M * 2], visited[M * 2];
int kk[Q], ans[Q], q;
void dfs(int i, int r) {
int o, h;
visited[i] = 1;
qu2[cnt2++] = i;
if (src[i])
for (h = 0; h < q; h++) {
int p = kk[h] < cnt2 ? qu2[cnt2 - 1 - kk[h]] : qu1[(r + kk[h] - (cnt2 - 1)) % cnt1];
if (dest[p])
ans[h]++;
}
for (o = fo[i]; o--; ) {
int j = fj[i][o];
if (visited[j] != 1)
dfs(j, r);
}
cnt2--;
}
void count_routes(int n, int m, int p, int ii[][2], int q_, int *kk_) {
static int pp[M * 2];
int h, i, j, o;
q = q_;
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++)
append(eh, eo, ii[h][0], h << 1 | 0), append(eh, eo, ii[h][1], h << 1 | 1);
for (i = 0; i < n; i++) {
int h1, h2;
h1 = INF, h2 = INF;
for (o = eo[i]; o--; ) {
h = eh[i][o];
if (h1 > h)
h2 = h1, h1 = h;
else if (h2 > h)
h2 = h;
}
src[h1] = 1;
if (h2 == INF)
pp[h1 ^ 1] = h1;
else {
pp[h1 ^ 1] = h2;
for (o = eo[i]; o--; ) {
h = eh[i][o];
if (h != h1)
pp[h ^ 1] = h1;
}
}
}
for (o = eo[p]; o--; ) {
h = eh[p][o];
dest[h ^ 1] = 1;
}
for (h = 0; h < q; h++)
kk[h] = kk_[h] - 1;
for (i = 0; i < m * 2; i++)
fj[i] = (int *) malloc(2 * sizeof *fj[i]);
for (i = 0; i < m * 2; i++)
append(fj, fo, pp[i], i);
for (i = 0; i < m * 2; i++)
if (!visited[i]) {
j = i;
while (!visited[j])
visited[j] = -1, j = pp[j];
cnt1 = 0;
while (visited[j] == -1) {
visited[j] = 1, j = pp[j];
qu1[cnt1++] = j;
}
cnt2 = 0;
for (h = 0; h < cnt1; h++)
dfs(qu1[h], h);
}
for (h = 0; h < q; h++)
answer(ans[h]);
}
Compilation message
garden.c: In function 'append':
garden.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
garden.c: In function 'count_routes':
garden.c:64:11: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
64 | src[h1] = 1;
| ~~~~~~~~^~~
garden.c:20:46: note: at offset 1061109567 to object 'src' with size 300000 declared here
20 | int qu1[M * 2], cnt1, qu2[M * 2], cnt2; char src[M * 2], dest[M * 2], visited[M * 2];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
4 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
4 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
13 ms |
3728 KB |
Output is correct |
12 |
Correct |
37 ms |
7600 KB |
Output is correct |
13 |
Correct |
37 ms |
13632 KB |
Output is correct |
14 |
Correct |
103 ms |
21572 KB |
Output is correct |
15 |
Correct |
112 ms |
22480 KB |
Output is correct |
16 |
Correct |
100 ms |
19308 KB |
Output is correct |
17 |
Correct |
120 ms |
18872 KB |
Output is correct |
18 |
Correct |
33 ms |
7708 KB |
Output is correct |
19 |
Correct |
112 ms |
21552 KB |
Output is correct |
20 |
Correct |
116 ms |
22276 KB |
Output is correct |
21 |
Correct |
106 ms |
19356 KB |
Output is correct |
22 |
Correct |
99 ms |
18820 KB |
Output is correct |
23 |
Correct |
115 ms |
23008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
4 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
13 ms |
3728 KB |
Output is correct |
12 |
Correct |
37 ms |
7600 KB |
Output is correct |
13 |
Correct |
37 ms |
13632 KB |
Output is correct |
14 |
Correct |
103 ms |
21572 KB |
Output is correct |
15 |
Correct |
112 ms |
22480 KB |
Output is correct |
16 |
Correct |
100 ms |
19308 KB |
Output is correct |
17 |
Correct |
120 ms |
18872 KB |
Output is correct |
18 |
Correct |
33 ms |
7708 KB |
Output is correct |
19 |
Correct |
112 ms |
21552 KB |
Output is correct |
20 |
Correct |
116 ms |
22276 KB |
Output is correct |
21 |
Correct |
106 ms |
19356 KB |
Output is correct |
22 |
Correct |
99 ms |
18820 KB |
Output is correct |
23 |
Correct |
115 ms |
23008 KB |
Output is correct |
24 |
Correct |
3 ms |
332 KB |
Output is correct |
25 |
Correct |
308 ms |
3660 KB |
Output is correct |
26 |
Correct |
565 ms |
7728 KB |
Output is correct |
27 |
Correct |
1299 ms |
14684 KB |
Output is correct |
28 |
Correct |
2783 ms |
23324 KB |
Output is correct |
29 |
Correct |
2954 ms |
24120 KB |
Output is correct |
30 |
Correct |
1676 ms |
20900 KB |
Output is correct |
31 |
Correct |
1670 ms |
20476 KB |
Output is correct |
32 |
Correct |
604 ms |
8268 KB |
Output is correct |
33 |
Correct |
2781 ms |
23316 KB |
Output is correct |
34 |
Correct |
2907 ms |
24116 KB |
Output is correct |
35 |
Correct |
1749 ms |
21068 KB |
Output is correct |
36 |
Correct |
1684 ms |
20548 KB |
Output is correct |
37 |
Correct |
2585 ms |
24828 KB |
Output is correct |
38 |
Correct |
2254 ms |
25080 KB |
Output is correct |