#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
#define M 400000
#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 aa[N], n;
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int dd[N];
int solve(int t) {
static char visited[N];
int i, i_, o;
for (i = 0; i < n; i++)
dd[i] = INF;
dd[0] = 0;
memset(visited, 0, n * sizeof *visited);
while (1) {
i_ = -1;
for (i = 0; i < n; i++)
if (!visited[i] && dd[i] != INF && (i_ == -1 || dd[i_] > dd[i]))
i_ = i;
i = i_;
if (i == -1)
return 0;
if (i == n - 1)
return 1;
visited[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], d = max(dd[i] + 1, aa[j]);
if (d + aa[j] <= t)
dd[j] = min(dd[j], d);
}
}
}
int main() {
int m, h, i, j, subtask, lower, upper;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
subtask = 1;
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
if (i != h || j != h + 1)
subtask = 3;
}
if (subtask == 1) {
int x, y;
x = 0;
for (i = 0; i < n; i++)
x = max(x, aa[i] - i);
y = 0;
for (i = 0; i < n; i++)
y = max(y, aa[i] - (n - 1 - i));
printf("%d\n", n - 1 + x + y);
} else {
lower = -1, upper = INF;
while (upper - lower > 1) {
int t = (lower + upper) / 2;
if (solve(t))
upper = t;
else
lower = t;
}
printf("%d\n", upper);
}
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:59:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
48 ms |
12764 KB |
Output is correct |
3 |
Correct |
49 ms |
12860 KB |
Output is correct |
4 |
Correct |
50 ms |
12956 KB |
Output is correct |
5 |
Correct |
50 ms |
12852 KB |
Output is correct |
6 |
Correct |
50 ms |
12876 KB |
Output is correct |
7 |
Correct |
64 ms |
12836 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
114 ms |
428 KB |
Output is correct |
7 |
Correct |
84 ms |
424 KB |
Output is correct |
8 |
Correct |
57 ms |
420 KB |
Output is correct |
9 |
Correct |
85 ms |
424 KB |
Output is correct |
10 |
Correct |
89 ms |
424 KB |
Output is correct |
11 |
Correct |
27 ms |
428 KB |
Output is correct |
12 |
Correct |
109 ms |
428 KB |
Output is correct |
13 |
Correct |
11 ms |
340 KB |
Output is correct |
14 |
Correct |
15 ms |
468 KB |
Output is correct |
15 |
Correct |
39 ms |
476 KB |
Output is correct |
16 |
Correct |
10 ms |
488 KB |
Output is correct |
17 |
Correct |
69 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
114 ms |
428 KB |
Output is correct |
7 |
Correct |
84 ms |
424 KB |
Output is correct |
8 |
Correct |
57 ms |
420 KB |
Output is correct |
9 |
Correct |
85 ms |
424 KB |
Output is correct |
10 |
Correct |
89 ms |
424 KB |
Output is correct |
11 |
Correct |
27 ms |
428 KB |
Output is correct |
12 |
Correct |
109 ms |
428 KB |
Output is correct |
13 |
Correct |
11 ms |
340 KB |
Output is correct |
14 |
Correct |
15 ms |
468 KB |
Output is correct |
15 |
Correct |
39 ms |
476 KB |
Output is correct |
16 |
Correct |
10 ms |
488 KB |
Output is correct |
17 |
Correct |
69 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
25 ms |
340 KB |
Output is correct |
20 |
Correct |
56 ms |
340 KB |
Output is correct |
21 |
Correct |
64 ms |
428 KB |
Output is correct |
22 |
Correct |
35 ms |
336 KB |
Output is correct |
23 |
Correct |
40 ms |
428 KB |
Output is correct |
24 |
Correct |
59 ms |
428 KB |
Output is correct |
25 |
Correct |
11 ms |
332 KB |
Output is correct |
26 |
Correct |
7 ms |
444 KB |
Output is correct |
27 |
Correct |
3 ms |
488 KB |
Output is correct |
28 |
Correct |
136 ms |
484 KB |
Output is correct |
29 |
Correct |
48 ms |
480 KB |
Output is correct |
30 |
Correct |
57 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
48 ms |
12764 KB |
Output is correct |
3 |
Correct |
49 ms |
12860 KB |
Output is correct |
4 |
Correct |
50 ms |
12956 KB |
Output is correct |
5 |
Correct |
50 ms |
12852 KB |
Output is correct |
6 |
Correct |
50 ms |
12876 KB |
Output is correct |
7 |
Correct |
64 ms |
12836 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
114 ms |
428 KB |
Output is correct |
15 |
Correct |
84 ms |
424 KB |
Output is correct |
16 |
Correct |
57 ms |
420 KB |
Output is correct |
17 |
Correct |
85 ms |
424 KB |
Output is correct |
18 |
Correct |
89 ms |
424 KB |
Output is correct |
19 |
Correct |
27 ms |
428 KB |
Output is correct |
20 |
Correct |
109 ms |
428 KB |
Output is correct |
21 |
Correct |
11 ms |
340 KB |
Output is correct |
22 |
Correct |
15 ms |
468 KB |
Output is correct |
23 |
Correct |
39 ms |
476 KB |
Output is correct |
24 |
Correct |
10 ms |
488 KB |
Output is correct |
25 |
Correct |
69 ms |
472 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
25 ms |
340 KB |
Output is correct |
28 |
Correct |
56 ms |
340 KB |
Output is correct |
29 |
Correct |
64 ms |
428 KB |
Output is correct |
30 |
Correct |
35 ms |
336 KB |
Output is correct |
31 |
Correct |
40 ms |
428 KB |
Output is correct |
32 |
Correct |
59 ms |
428 KB |
Output is correct |
33 |
Correct |
11 ms |
332 KB |
Output is correct |
34 |
Correct |
7 ms |
444 KB |
Output is correct |
35 |
Correct |
3 ms |
488 KB |
Output is correct |
36 |
Correct |
136 ms |
484 KB |
Output is correct |
37 |
Correct |
48 ms |
480 KB |
Output is correct |
38 |
Correct |
57 ms |
488 KB |
Output is correct |
39 |
Execution timed out |
1067 ms |
15032 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |