#include "dreaming.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int *ej[N], *ew[N], eo[N];
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
char rr[N]; int dp[N], ans;
void dfs1(int p, int i, int r) {
int o, d1, d2;
if (rr[i] != -1)
return;
rr[i] = r, d1 = 0, d2 = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p) {
dfs1(i, j, r);
if (d1 < dp[j] + w)
d2 = d1, d1 = dp[j] + w;
else if (d2 < dp[j] + w)
d2 = dp[j] + w;
}
}
ans = max(ans, d1 + d2);
dp[i] = d1;
}
int r;
void dfs2(int p, int i, int d) {
int o, d1, d2, j1;
r = min(r, max(dp[i], d));
d1 = 0, d2 = 0, j1 = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p) {
if (d1 < dp[j] + w)
d2 = d1, d1 = dp[j] + w, j1 = j;
else if (d2 < dp[j] + w)
d2 = dp[j] + w;
}
}
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p)
dfs2(i, j, max(d, j == j1 ? d2 : d1) + w);
}
}
int travelTime(int n, int m, int l, int ii[], int jj[], int ww[]) {
int h, i, r1, r2;
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
for (h = 0; h < m; h++)
append(ii[h], jj[h], ww[h]), append(jj[h], ii[h], ww[h]);
if (m < n - 2)
return -1;
memset(rr, -1, n * sizeof *rr);
ans = 0;
r = INF, dfs1(-1, 0, 0), dfs2(-1, 0, 0), r1 = r;
if (m == n - 1)
return ans;
r2 = 0;
for (i = 0; i < n; i++)
if (rr[i] == -1) {
r = INF, dfs1(-1, i, i), dfs2(-1, i, 0), r2 = r;
break;
}
ans = max(ans, r1 + l + r2);
return ans;
}
Compilation message
dreaming.c: In function 'append':
dreaming.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
15172 KB |
Output is correct |
2 |
Correct |
66 ms |
14824 KB |
Output is correct |
3 |
Correct |
40 ms |
11964 KB |
Output is correct |
4 |
Correct |
8 ms |
2508 KB |
Output is correct |
5 |
Correct |
6 ms |
1684 KB |
Output is correct |
6 |
Correct |
11 ms |
3660 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
22 ms |
6528 KB |
Output is correct |
9 |
Correct |
33 ms |
9500 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
47 ms |
11456 KB |
Output is correct |
12 |
Correct |
57 ms |
13252 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
15172 KB |
Output is correct |
2 |
Correct |
66 ms |
14824 KB |
Output is correct |
3 |
Correct |
40 ms |
11964 KB |
Output is correct |
4 |
Correct |
8 ms |
2508 KB |
Output is correct |
5 |
Correct |
6 ms |
1684 KB |
Output is correct |
6 |
Correct |
11 ms |
3660 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
22 ms |
6528 KB |
Output is correct |
9 |
Correct |
33 ms |
9500 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
47 ms |
11456 KB |
Output is correct |
12 |
Correct |
57 ms |
13252 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
296 KB |
Output is correct |
28 |
Correct |
0 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
72 ms |
15160 KB |
Output is correct |
40 |
Correct |
69 ms |
14824 KB |
Output is correct |
41 |
Correct |
41 ms |
11964 KB |
Output is correct |
42 |
Correct |
7 ms |
2380 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
0 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
0 ms |
288 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
0 ms |
332 KB |
Output is correct |
56 |
Correct |
0 ms |
332 KB |
Output is correct |
57 |
Correct |
73 ms |
11592 KB |
Output is correct |
58 |
Correct |
70 ms |
11432 KB |
Output is correct |
59 |
Correct |
71 ms |
11636 KB |
Output is correct |
60 |
Correct |
71 ms |
11664 KB |
Output is correct |
61 |
Correct |
79 ms |
11960 KB |
Output is correct |
62 |
Correct |
71 ms |
11972 KB |
Output is correct |
63 |
Correct |
80 ms |
11732 KB |
Output is correct |
64 |
Correct |
77 ms |
11756 KB |
Output is correct |
65 |
Correct |
95 ms |
12028 KB |
Output is correct |
66 |
Correct |
77 ms |
12064 KB |
Output is correct |
67 |
Correct |
90 ms |
11700 KB |
Output is correct |
68 |
Correct |
73 ms |
11628 KB |
Output is correct |
69 |
Correct |
77 ms |
11936 KB |
Output is correct |
70 |
Correct |
74 ms |
11856 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
2 ms |
588 KB |
Output is correct |
73 |
Correct |
2 ms |
588 KB |
Output is correct |
74 |
Correct |
2 ms |
596 KB |
Output is correct |
75 |
Correct |
2 ms |
580 KB |
Output is correct |
76 |
Correct |
2 ms |
604 KB |
Output is correct |
77 |
Correct |
2 ms |
576 KB |
Output is correct |
78 |
Correct |
2 ms |
604 KB |
Output is correct |
79 |
Correct |
2 ms |
600 KB |
Output is correct |
80 |
Correct |
71 ms |
11656 KB |
Output is correct |
81 |
Correct |
71 ms |
11676 KB |
Output is correct |
82 |
Correct |
78 ms |
12020 KB |
Output is correct |
83 |
Correct |
73 ms |
12024 KB |
Output is correct |
84 |
Correct |
1 ms |
588 KB |
Output is correct |
85 |
Correct |
1 ms |
464 KB |
Output is correct |
86 |
Correct |
1 ms |
588 KB |
Output is correct |
87 |
Correct |
2 ms |
604 KB |
Output is correct |
88 |
Correct |
2 ms |
596 KB |
Output is correct |
89 |
Correct |
2 ms |
604 KB |
Output is correct |
90 |
Correct |
2 ms |
604 KB |
Output is correct |
91 |
Correct |
2 ms |
604 KB |
Output is correct |
92 |
Correct |
2 ms |
604 KB |
Output is correct |
93 |
Correct |
2 ms |
588 KB |
Output is correct |
94 |
Correct |
0 ms |
332 KB |
Output is correct |
95 |
Correct |
0 ms |
332 KB |
Output is correct |
96 |
Correct |
0 ms |
332 KB |
Output is correct |
97 |
Correct |
1 ms |
332 KB |
Output is correct |
98 |
Correct |
0 ms |
332 KB |
Output is correct |
99 |
Correct |
0 ms |
332 KB |
Output is correct |
100 |
Correct |
0 ms |
204 KB |
Output is correct |
101 |
Correct |
0 ms |
332 KB |
Output is correct |
102 |
Correct |
0 ms |
332 KB |
Output is correct |
103 |
Correct |
0 ms |
332 KB |
Output is correct |
104 |
Correct |
5 ms |
1612 KB |
Output is correct |
105 |
Correct |
12 ms |
3592 KB |
Output is correct |
106 |
Correct |
1 ms |
332 KB |
Output is correct |
107 |
Correct |
23 ms |
6592 KB |
Output is correct |
108 |
Correct |
32 ms |
9540 KB |
Output is correct |
109 |
Correct |
1 ms |
424 KB |
Output is correct |
110 |
Correct |
47 ms |
11376 KB |
Output is correct |
111 |
Correct |
58 ms |
13216 KB |
Output is correct |
112 |
Correct |
1 ms |
460 KB |
Output is correct |
113 |
Correct |
89 ms |
20972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
8688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
15172 KB |
Output is correct |
2 |
Correct |
66 ms |
14824 KB |
Output is correct |
3 |
Correct |
40 ms |
11964 KB |
Output is correct |
4 |
Correct |
8 ms |
2508 KB |
Output is correct |
5 |
Correct |
6 ms |
1684 KB |
Output is correct |
6 |
Correct |
11 ms |
3660 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
22 ms |
6528 KB |
Output is correct |
9 |
Correct |
33 ms |
9500 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
47 ms |
11456 KB |
Output is correct |
12 |
Correct |
57 ms |
13252 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
296 KB |
Output is correct |
28 |
Correct |
0 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
72 ms |
15160 KB |
Output is correct |
40 |
Correct |
69 ms |
14824 KB |
Output is correct |
41 |
Correct |
41 ms |
11964 KB |
Output is correct |
42 |
Correct |
7 ms |
2380 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
0 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
0 ms |
288 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
0 ms |
332 KB |
Output is correct |
56 |
Correct |
0 ms |
332 KB |
Output is correct |
57 |
Correct |
73 ms |
11592 KB |
Output is correct |
58 |
Correct |
70 ms |
11432 KB |
Output is correct |
59 |
Correct |
71 ms |
11636 KB |
Output is correct |
60 |
Correct |
71 ms |
11664 KB |
Output is correct |
61 |
Correct |
79 ms |
11960 KB |
Output is correct |
62 |
Correct |
71 ms |
11972 KB |
Output is correct |
63 |
Correct |
80 ms |
11732 KB |
Output is correct |
64 |
Correct |
77 ms |
11756 KB |
Output is correct |
65 |
Correct |
95 ms |
12028 KB |
Output is correct |
66 |
Correct |
77 ms |
12064 KB |
Output is correct |
67 |
Correct |
90 ms |
11700 KB |
Output is correct |
68 |
Correct |
73 ms |
11628 KB |
Output is correct |
69 |
Correct |
77 ms |
11936 KB |
Output is correct |
70 |
Correct |
74 ms |
11856 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
2 ms |
588 KB |
Output is correct |
73 |
Correct |
2 ms |
588 KB |
Output is correct |
74 |
Correct |
2 ms |
596 KB |
Output is correct |
75 |
Correct |
2 ms |
580 KB |
Output is correct |
76 |
Correct |
2 ms |
604 KB |
Output is correct |
77 |
Correct |
2 ms |
576 KB |
Output is correct |
78 |
Correct |
2 ms |
604 KB |
Output is correct |
79 |
Correct |
2 ms |
600 KB |
Output is correct |
80 |
Correct |
71 ms |
11656 KB |
Output is correct |
81 |
Correct |
71 ms |
11676 KB |
Output is correct |
82 |
Correct |
78 ms |
12020 KB |
Output is correct |
83 |
Correct |
73 ms |
12024 KB |
Output is correct |
84 |
Correct |
1 ms |
588 KB |
Output is correct |
85 |
Correct |
1 ms |
464 KB |
Output is correct |
86 |
Correct |
1 ms |
588 KB |
Output is correct |
87 |
Correct |
2 ms |
604 KB |
Output is correct |
88 |
Correct |
2 ms |
596 KB |
Output is correct |
89 |
Correct |
2 ms |
604 KB |
Output is correct |
90 |
Correct |
2 ms |
604 KB |
Output is correct |
91 |
Correct |
2 ms |
604 KB |
Output is correct |
92 |
Correct |
2 ms |
604 KB |
Output is correct |
93 |
Correct |
2 ms |
588 KB |
Output is correct |
94 |
Correct |
0 ms |
332 KB |
Output is correct |
95 |
Correct |
0 ms |
332 KB |
Output is correct |
96 |
Correct |
0 ms |
332 KB |
Output is correct |
97 |
Correct |
1 ms |
332 KB |
Output is correct |
98 |
Correct |
0 ms |
332 KB |
Output is correct |
99 |
Correct |
0 ms |
332 KB |
Output is correct |
100 |
Correct |
0 ms |
204 KB |
Output is correct |
101 |
Correct |
0 ms |
332 KB |
Output is correct |
102 |
Correct |
0 ms |
332 KB |
Output is correct |
103 |
Correct |
0 ms |
332 KB |
Output is correct |
104 |
Correct |
5 ms |
1612 KB |
Output is correct |
105 |
Correct |
12 ms |
3592 KB |
Output is correct |
106 |
Correct |
1 ms |
332 KB |
Output is correct |
107 |
Correct |
23 ms |
6592 KB |
Output is correct |
108 |
Correct |
32 ms |
9540 KB |
Output is correct |
109 |
Correct |
1 ms |
424 KB |
Output is correct |
110 |
Correct |
47 ms |
11376 KB |
Output is correct |
111 |
Correct |
58 ms |
13216 KB |
Output is correct |
112 |
Correct |
1 ms |
460 KB |
Output is correct |
113 |
Correct |
89 ms |
20972 KB |
Output is correct |
114 |
Incorrect |
20 ms |
8688 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |