This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |