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 200000
#define MD 1000000007
void append(int **ej, int *eo, 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 main() {
static int ww[N], *ej[N], eo[N], *fi[N], fo[N], dp[N], dq[N];
int n, m, h, i, j, o, sum, ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d", &ww[i]);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
fi[i] = (int *) malloc(2 * sizeof *fi[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(ej, eo, i, j), append(fi, fo, j, i);
}
sum = 0;
for (j = 0; j < n; j++) {
dp[j] = (sum + 1) % MD;
for (o = fo[j]; o--; ) {
i = fi[j][o];
dp[j] = (dp[j] - dp[i] + MD) % MD;
}
sum = (sum + dp[j]) % MD;
}
sum = 0;
for (i = n - 1; i >= 0; i--) {
dq[i] = (sum + 1) % MD;
for (o = eo[i]; o--; ) {
j = ej[i][o];
dq[i] = (dq[i] - dq[j] + MD) % MD;
}
sum = (sum + dq[i]) % MD;
}
ans = 0;
for (i = 0; i < n; i++)
ans = (ans + ww[i] * (((long long) dp[i] * dq[i] - 1 + MD) % MD)) % MD;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
keliones.c: In function 'append':
keliones.c:10:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
10 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
keliones.c: In function 'main':
keliones.c:19:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
keliones.c:21:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d", &ww[i]);
| ^~~~~~~~~~~~~~~~~~~
keliones.c:27:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | 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... |