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>
#define N 5000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
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 solve(int n, int s) {
static int dd[N], qu[N];
int i, head, cnt, ans;
for (i = 0; i < n; i++)
dd[i] = n + 1;
head = cnt = 0;
dd[s] = 1, qu[head + cnt++] = s;
while (cnt) {
int d, o;
i = qu[cnt--, head++], d = dd[i] + 1;
for (o = 0; o < eo[i]; o++) {
int j = ej[i][o];
if (dd[j] > d)
dd[j] = d, qu[head + cnt++] = j;
}
}
if (head != n)
return INF;
ans = 0;
for (i = 0; i < n; i++)
ans += dd[i];
return ans;
}
int main() {
int n, i, ans;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n; i++) {
int m;
scanf("%d", &m);
while (m--) {
int j;
scanf("%d", &j), j--;
append(j, i);
}
}
ans = INF;
for (i = 0; i < n; i++)
ans = min(ans, solve(n, i));
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
bosses.c: In function 'append':
bosses.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
bosses.c: In function 'main':
bosses.c:49:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
bosses.c:55:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &m);
| ^~~~~~~~~~~~~~~
bosses.c:59:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &j), 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... |