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 "traffic.h"
#include <stdlib.h>
#define N 1000000
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 sz[N], sum, c;
void dfs(int p, int i) {
int o;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
sz[i] += sz[j];
}
}
if (sz[i] >= (sum + 1) / 2 && (c == -1 || sz[c] > sz[i]))
c = i;
}
int LocateCentre(int n, int *aa, int *ii, int *jj) {
int h, i;
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++)
append(ii[h], jj[h]), append(jj[h], ii[h]);
sum = 0;
for (i = 0; i < n; i++)
sum += aa[i], sz[i] = aa[i];
c = -1, dfs(-1, 0);
return c;
}
Compilation message (stderr)
traffic.c: In function 'append':
traffic.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
# | 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... |