#include "race.h"
#include <stdlib.h>
#include <string.h>
#define N 200000
#define L 1000000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int ij[N - 1], ww[N - 1];
int *eh[N], eo[N];
void append(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 sz[N], c;
void dfs0(int p, int i, int n) {
int o, centroid;
sz[i] = 1, centroid = 1;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
if (j != p) {
dfs0(i, j, n);
if (sz[j] * 2 > n)
centroid = 0;
sz[i] += sz[j];
}
}
if ((n - sz[i]) * 2 > n)
centroid = 0;
if (centroid)
c = i;
}
int xx[N], dd[N], dd_[L + 1], qu[N], l, cnt, ans;
void dfs1(int p, int i, int x, int d) {
int o;
xx[i] = x, dd[i] = d;
if (x <= l)
ans = min(ans, dd_[l - x] + d);
qu[cnt++] = i;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
if (j != p)
dfs1(i, j, min(x + ww[h], l + 1), d + 1);
}
}
void delete(int i, int h) {
int o;
for (o = eo[i]; o--; )
if (eh[i][o] == h) {
eo[i]--;
while (o < eo[i])
eh[i][o] = eh[i][o + 1], o++;
return;
}
}
void cd(int i, int n) {
int o, g;
dfs0(-1, i, n), i = c;
cnt = 0;
xx[i] = 0, dd[i] = 0;
qu[cnt++] = i, dd_[xx[i]] = dd[i];
for (o = eo[i], g = 0; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
delete(j, h);
dfs1(i, j, min(ww[h], l + 1), 1);
while (g < cnt) {
j = qu[g++];
if (xx[j] <= l)
dd_[xx[j]] = min(dd_[xx[j]], dd[j]);
}
}
while (cnt--) {
int j = qu[cnt];
if (xx[j] <= l)
dd_[xx[j]] = INF;
}
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
cd(j, sz[j] < sz[i] ? sz[j] : n - sz[i]);
}
}
int best_path(int n, int l_, int ii_[][2], int *ww_) {
int h, i, j;
memcpy(ww, ww_, (n - 1) * sizeof *ww_), l = l_;
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < n - 1; h++) {
i = ii_[h][0], j = ii_[h][1];
ij[h] = i ^ j;
append(i, h), append(j, h);
}
memset(dd_, 0x3f, (l + 1) * sizeof *dd_);
ans = INF;
cd(0, n);
if (ans == INF)
ans = -1;
return ans;
}
Compilation message
race.c: In function 'append':
race.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
4032 KB |
Output is correct |
23 |
Correct |
3 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3788 KB |
Output is correct |
25 |
Correct |
3 ms |
3660 KB |
Output is correct |
26 |
Correct |
2 ms |
1700 KB |
Output is correct |
27 |
Correct |
3 ms |
3492 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1828 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
3 ms |
3404 KB |
Output is correct |
34 |
Correct |
2 ms |
2720 KB |
Output is correct |
35 |
Correct |
3 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
4004 KB |
Output is correct |
37 |
Correct |
3 ms |
3532 KB |
Output is correct |
38 |
Correct |
2 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
184 ms |
9624 KB |
Output is correct |
20 |
Correct |
200 ms |
9624 KB |
Output is correct |
21 |
Correct |
212 ms |
9668 KB |
Output is correct |
22 |
Correct |
180 ms |
9812 KB |
Output is correct |
23 |
Correct |
200 ms |
9572 KB |
Output is correct |
24 |
Correct |
93 ms |
10052 KB |
Output is correct |
25 |
Correct |
211 ms |
13604 KB |
Output is correct |
26 |
Correct |
123 ms |
17224 KB |
Output is correct |
27 |
Correct |
171 ms |
19576 KB |
Output is correct |
28 |
Correct |
681 ms |
34628 KB |
Output is correct |
29 |
Correct |
707 ms |
33472 KB |
Output is correct |
30 |
Correct |
160 ms |
19628 KB |
Output is correct |
31 |
Correct |
164 ms |
19572 KB |
Output is correct |
32 |
Correct |
271 ms |
19676 KB |
Output is correct |
33 |
Correct |
525 ms |
19004 KB |
Output is correct |
34 |
Correct |
459 ms |
19872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
4032 KB |
Output is correct |
23 |
Correct |
3 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3788 KB |
Output is correct |
25 |
Correct |
3 ms |
3660 KB |
Output is correct |
26 |
Correct |
2 ms |
1700 KB |
Output is correct |
27 |
Correct |
3 ms |
3492 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1828 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
3 ms |
3404 KB |
Output is correct |
34 |
Correct |
2 ms |
2720 KB |
Output is correct |
35 |
Correct |
3 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
4004 KB |
Output is correct |
37 |
Correct |
3 ms |
3532 KB |
Output is correct |
38 |
Correct |
2 ms |
2380 KB |
Output is correct |
39 |
Correct |
184 ms |
9624 KB |
Output is correct |
40 |
Correct |
200 ms |
9624 KB |
Output is correct |
41 |
Correct |
212 ms |
9668 KB |
Output is correct |
42 |
Correct |
180 ms |
9812 KB |
Output is correct |
43 |
Correct |
200 ms |
9572 KB |
Output is correct |
44 |
Correct |
93 ms |
10052 KB |
Output is correct |
45 |
Correct |
211 ms |
13604 KB |
Output is correct |
46 |
Correct |
123 ms |
17224 KB |
Output is correct |
47 |
Correct |
171 ms |
19576 KB |
Output is correct |
48 |
Correct |
681 ms |
34628 KB |
Output is correct |
49 |
Correct |
707 ms |
33472 KB |
Output is correct |
50 |
Correct |
160 ms |
19628 KB |
Output is correct |
51 |
Correct |
164 ms |
19572 KB |
Output is correct |
52 |
Correct |
271 ms |
19676 KB |
Output is correct |
53 |
Correct |
525 ms |
19004 KB |
Output is correct |
54 |
Correct |
459 ms |
19872 KB |
Output is correct |
55 |
Correct |
10 ms |
1240 KB |
Output is correct |
56 |
Correct |
11 ms |
1240 KB |
Output is correct |
57 |
Correct |
93 ms |
9364 KB |
Output is correct |
58 |
Correct |
37 ms |
9612 KB |
Output is correct |
59 |
Correct |
105 ms |
18028 KB |
Output is correct |
60 |
Correct |
737 ms |
37660 KB |
Output is correct |
61 |
Correct |
162 ms |
19752 KB |
Output is correct |
62 |
Correct |
169 ms |
23476 KB |
Output is correct |
63 |
Correct |
305 ms |
23576 KB |
Output is correct |
64 |
Correct |
641 ms |
21696 KB |
Output is correct |
65 |
Correct |
421 ms |
20380 KB |
Output is correct |
66 |
Correct |
799 ms |
35012 KB |
Output is correct |
67 |
Correct |
153 ms |
24064 KB |
Output is correct |
68 |
Correct |
317 ms |
24212 KB |
Output is correct |
69 |
Correct |
343 ms |
24320 KB |
Output is correct |
70 |
Correct |
318 ms |
23264 KB |
Output is correct |