#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; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int n;
int aa[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
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 qu1[N], cnt1; char in1[N];
int qu2[N], cnt2;
int solve(int t) {
int h1, h2, i, o;
memset(dd, 0x3f, n * sizeof *dd);
memset(in1, 0, n * sizeof *in1);
dd[0] = 0, in1[0] = 1;
cnt1 = n, cnt2 = 0, h1 = 0, h2 = 0, i = 0;
while (h1 < cnt1 || h2 < cnt2) {
if (h2 == cnt2 || h1 < cnt1 && aa[qu1[h1]] < dd[qu2[h2]]) {
i = qu1[h1++];
if (!in1[i])
continue;
} else
i = qu2[h2++];
if (i == n - 1)
return 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] > d) {
dd[j] = d;
if (d == aa[j])
in1[j] = 1;
else
qu2[cnt2++] = j;
}
}
}
return 0;
}
int main() {
static int ii[N];
int m, h, i, j, lower, upper;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]);
ii[i] = i;
}
sort(ii, 0, n);
for (i = 0; i < n; i++)
qu1[i] = ii[i];
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
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:46:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
46 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'solve':
Main.c:63:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
63 | if (h2 == cnt2 || h1 < cnt1 && aa[qu1[h1]] < dd[qu2[h2]]) {
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:90:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:92:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
109 ms |
13236 KB |
Output is correct |
3 |
Correct |
122 ms |
13412 KB |
Output is correct |
4 |
Correct |
135 ms |
13392 KB |
Output is correct |
5 |
Correct |
121 ms |
13364 KB |
Output is correct |
6 |
Correct |
124 ms |
13376 KB |
Output is correct |
7 |
Correct |
122 ms |
13344 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
428 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
428 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
428 KB |
Output is correct |
17 |
Correct |
2 ms |
528 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
428 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
428 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
428 KB |
Output is correct |
17 |
Correct |
2 ms |
528 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
432 KB |
Output is correct |
23 |
Correct |
1 ms |
476 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
109 ms |
13236 KB |
Output is correct |
3 |
Correct |
122 ms |
13412 KB |
Output is correct |
4 |
Correct |
135 ms |
13392 KB |
Output is correct |
5 |
Correct |
121 ms |
13364 KB |
Output is correct |
6 |
Correct |
124 ms |
13376 KB |
Output is correct |
7 |
Correct |
122 ms |
13344 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
428 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
428 KB |
Output is correct |
25 |
Correct |
2 ms |
528 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
432 KB |
Output is correct |
31 |
Correct |
1 ms |
476 KB |
Output is correct |
32 |
Correct |
1 ms |
440 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
432 KB |
Output is correct |
39 |
Correct |
233 ms |
12912 KB |
Output is correct |
40 |
Correct |
199 ms |
16352 KB |
Output is correct |
41 |
Correct |
182 ms |
16500 KB |
Output is correct |
42 |
Correct |
224 ms |
16348 KB |
Output is correct |
43 |
Correct |
190 ms |
16508 KB |
Output is correct |
44 |
Correct |
134 ms |
16220 KB |
Output is correct |
45 |
Correct |
110 ms |
15860 KB |
Output is correct |
46 |
Correct |
204 ms |
16516 KB |
Output is correct |
47 |
Correct |
116 ms |
16072 KB |
Output is correct |
48 |
Correct |
207 ms |
16520 KB |
Output is correct |
49 |
Correct |
202 ms |
17184 KB |
Output is correct |
50 |
Correct |
171 ms |
16900 KB |
Output is correct |
51 |
Correct |
263 ms |
17196 KB |
Output is correct |
52 |
Correct |
461 ms |
19636 KB |
Output is correct |
53 |
Correct |
346 ms |
18116 KB |
Output is correct |
54 |
Correct |
98 ms |
17748 KB |
Output is correct |
55 |
Correct |
379 ms |
18244 KB |
Output is correct |
56 |
Correct |
169 ms |
20440 KB |
Output is correct |
57 |
Correct |
490 ms |
20824 KB |
Output is correct |
58 |
Correct |
150 ms |
21544 KB |
Output is correct |
59 |
Correct |
180 ms |
21532 KB |
Output is correct |
60 |
Correct |
181 ms |
21556 KB |
Output is correct |
61 |
Correct |
476 ms |
21324 KB |
Output is correct |
62 |
Correct |
174 ms |
20316 KB |
Output is correct |
63 |
Correct |
564 ms |
20880 KB |
Output is correct |
64 |
Correct |
172 ms |
20532 KB |
Output is correct |
65 |
Correct |
359 ms |
20928 KB |
Output is correct |
66 |
Correct |
191 ms |
20496 KB |
Output is correct |
67 |
Correct |
436 ms |
20880 KB |
Output is correct |