#include <stdio.h>
#include <assert.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MOD = 1000000000 + 7;
const int MAXN = 2 * 100000 + 100;
vector<int> E[MAXN][2]; // E[i][0] saugo išimtas iš grafo briaunas, kurios prasideda viršūnėje i,
// o E[i][1] saugo išimtas iš grafo briaunas, kurios baigiasi viršūnėje i.
ll W[MAXN]; // Viršūnių svoriai.
ll A[MAXN]; // A[i] - kelių, pasibaigiančių viršūnėje i, skaičius.
ll B[MAXN]; // B[i] - kelių, prasidedančių viršūnėje i, skaičius.
int N, M;
int main() {
scanf("%d %d", &N, &M);
for (int i = 0;i < N;++i) {
scanf("%lld", &W[i]);
}
for (int i = 0;i < M;++i) {
int a, b;
scanf("%d %d", &a, &b);
// Numeruojame nuo 0.
a--;
b--;
E[a][0].push_back(b);
E[b][1].push_back(a);
}
// Kelių, pasibaigiančių nulinėje viršūnėje, skaičius yra 0, nes kelias turi turėti bent dvi viršūnes.
A[0] = 0;
// A[i] = (A[0] + 1) + (A[1] + 1) + .. + (A[i - 1] + 1) - (A[x1] + 1) - (A[x2] + 1) - .. - (A[xk] + 1)
// kur x1, x2, .., xk yra viršūnės, iš kurių briaunos į viršūnę i neina, t.y. buvo išimtos.
// Kintamajame s saugome (A[0] + 1) + (A[1] + 1) + .. + (A[i - 1] + 1) reikšmę.
ll s = 1;
for (int i = 1;i < N;++i) {
A[i] = s;
for (int j = 0;j < E[i][1].size();++j) {
int u = E[i][1][j];
A[i] = (A[i] - A[u] - 1) % MOD;
}
s = (s + A[i] + 1) % MOD;
}
// Analogiškai, B[i] = (B[N - 1] + 1) + .. + (B[i + 1] + 1) - (B[x1] + 1) - .. - (B[xk] + 1),
// kur x1, .., xk yra viršūnės, į kurias briaunos iš viršūnės i nėra, t.y. buvo išimtos.
B[N - 1] = 0;
s = 1;
for (int i = N - 2;i >= 0;--i) {
B[i] = s;
for (int j = 0;j < E[i][0].size();++j) {
int u = E[i][0][j];
B[i] = (B[i] - B[u] - 1) % MOD;
}
s = (s + B[i] + 1) % MOD;
}
// Jei A[i] - visų kelių, pasibaigiančių viršūnėje i, skaičius, o B[i] - visų kelių, prasidedančių viršūnėje i, skaičius, tai
// A[i] * B[i] + A[i] + B[i] - visų kelių einančių per viršūnę i skaičius.
// Todėl atsakymas yra suma per visus i W[i] * (A[i] * B[i] + A[i] + B[i])
ll answer = 0;
// Išnaudojame tapatybę A[i] * B[i] + A[i] + B[i] = (A[i] + 1) * (B[i] + 1) - 1.
for (int i = 0;i < N;++i) {
answer = ((W[i] * ((A[i] + 1) * (B[i] + 1) % MOD) % MOD) + answer) % MOD;
}
for (int i = 0;i < N;++i) {
answer = (answer - W[i]) % MOD;
}
answer = (answer + MOD) % MOD;
printf("%lld\n", answer);
return 0;
}
Compilation message
keliones.cpp: In function 'int main()':
keliones.cpp:54:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0;j < E[i][1].size();++j) {
~~^~~~~~~~~~~~~~~~
keliones.cpp:71:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0;j < E[i][0].size();++j) {
~~^~~~~~~~~~~~~~~~
keliones.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
keliones.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &W[i]);
~~~~~^~~~~~~~~~~~~~~
keliones.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9720 KB |
Output is correct |
4 |
Correct |
11 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9720 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9720 KB |
Output is correct |
9 |
Correct |
12 ms |
9720 KB |
Output is correct |
10 |
Correct |
13 ms |
9720 KB |
Output is correct |
11 |
Correct |
12 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
10744 KB |
Output is correct |
2 |
Correct |
37 ms |
10872 KB |
Output is correct |
3 |
Correct |
32 ms |
10744 KB |
Output is correct |
4 |
Correct |
19 ms |
9976 KB |
Output is correct |
5 |
Correct |
31 ms |
10616 KB |
Output is correct |
6 |
Correct |
24 ms |
10232 KB |
Output is correct |
7 |
Correct |
13 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9780 KB |
Output is correct |
9 |
Correct |
12 ms |
9720 KB |
Output is correct |
10 |
Correct |
12 ms |
9720 KB |
Output is correct |
11 |
Correct |
12 ms |
9720 KB |
Output is correct |
12 |
Correct |
12 ms |
9720 KB |
Output is correct |
13 |
Correct |
12 ms |
9720 KB |
Output is correct |
14 |
Correct |
12 ms |
9720 KB |
Output is correct |
15 |
Correct |
15 ms |
9848 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
11 ms |
9720 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9720 KB |
Output is correct |
20 |
Correct |
11 ms |
9720 KB |
Output is correct |
21 |
Correct |
11 ms |
9720 KB |
Output is correct |
22 |
Correct |
11 ms |
9720 KB |
Output is correct |
23 |
Correct |
11 ms |
9720 KB |
Output is correct |
24 |
Correct |
12 ms |
9720 KB |
Output is correct |
25 |
Correct |
13 ms |
9720 KB |
Output is correct |
26 |
Correct |
12 ms |
9720 KB |
Output is correct |
27 |
Correct |
11 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
10744 KB |
Output is correct |
2 |
Correct |
37 ms |
10872 KB |
Output is correct |
3 |
Correct |
32 ms |
10744 KB |
Output is correct |
4 |
Correct |
19 ms |
9976 KB |
Output is correct |
5 |
Correct |
31 ms |
10616 KB |
Output is correct |
6 |
Correct |
24 ms |
10232 KB |
Output is correct |
7 |
Correct |
13 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9780 KB |
Output is correct |
9 |
Correct |
12 ms |
9720 KB |
Output is correct |
10 |
Correct |
12 ms |
9720 KB |
Output is correct |
11 |
Correct |
12 ms |
9720 KB |
Output is correct |
12 |
Correct |
12 ms |
9720 KB |
Output is correct |
13 |
Correct |
12 ms |
9720 KB |
Output is correct |
14 |
Correct |
12 ms |
9720 KB |
Output is correct |
15 |
Correct |
15 ms |
9848 KB |
Output is correct |
16 |
Correct |
47 ms |
11256 KB |
Output is correct |
17 |
Correct |
19 ms |
10232 KB |
Output is correct |
18 |
Correct |
31 ms |
10612 KB |
Output is correct |
19 |
Correct |
20 ms |
10104 KB |
Output is correct |
20 |
Correct |
13 ms |
9848 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
32 ms |
10616 KB |
Output is correct |
23 |
Correct |
42 ms |
11128 KB |
Output is correct |
24 |
Correct |
46 ms |
11256 KB |
Output is correct |
25 |
Correct |
11 ms |
9720 KB |
Output is correct |
26 |
Correct |
11 ms |
9720 KB |
Output is correct |
27 |
Correct |
11 ms |
9720 KB |
Output is correct |
28 |
Correct |
11 ms |
9720 KB |
Output is correct |
29 |
Correct |
11 ms |
9720 KB |
Output is correct |
30 |
Correct |
11 ms |
9720 KB |
Output is correct |
31 |
Correct |
11 ms |
9720 KB |
Output is correct |
32 |
Correct |
11 ms |
9720 KB |
Output is correct |
33 |
Correct |
12 ms |
9720 KB |
Output is correct |
34 |
Correct |
13 ms |
9720 KB |
Output is correct |
35 |
Correct |
12 ms |
9720 KB |
Output is correct |
36 |
Correct |
11 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
10744 KB |
Output is correct |
2 |
Correct |
37 ms |
10872 KB |
Output is correct |
3 |
Correct |
32 ms |
10744 KB |
Output is correct |
4 |
Correct |
19 ms |
9976 KB |
Output is correct |
5 |
Correct |
31 ms |
10616 KB |
Output is correct |
6 |
Correct |
24 ms |
10232 KB |
Output is correct |
7 |
Correct |
13 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9780 KB |
Output is correct |
9 |
Correct |
12 ms |
9720 KB |
Output is correct |
10 |
Correct |
12 ms |
9720 KB |
Output is correct |
11 |
Correct |
12 ms |
9720 KB |
Output is correct |
12 |
Correct |
12 ms |
9720 KB |
Output is correct |
13 |
Correct |
12 ms |
9720 KB |
Output is correct |
14 |
Correct |
12 ms |
9720 KB |
Output is correct |
15 |
Correct |
15 ms |
9848 KB |
Output is correct |
16 |
Correct |
47 ms |
11256 KB |
Output is correct |
17 |
Correct |
19 ms |
10232 KB |
Output is correct |
18 |
Correct |
31 ms |
10612 KB |
Output is correct |
19 |
Correct |
20 ms |
10104 KB |
Output is correct |
20 |
Correct |
13 ms |
9848 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
32 ms |
10616 KB |
Output is correct |
23 |
Correct |
42 ms |
11128 KB |
Output is correct |
24 |
Correct |
46 ms |
11256 KB |
Output is correct |
25 |
Correct |
11 ms |
9720 KB |
Output is correct |
26 |
Correct |
11 ms |
9720 KB |
Output is correct |
27 |
Correct |
11 ms |
9720 KB |
Output is correct |
28 |
Correct |
11 ms |
9720 KB |
Output is correct |
29 |
Correct |
11 ms |
9720 KB |
Output is correct |
30 |
Correct |
11 ms |
9720 KB |
Output is correct |
31 |
Correct |
11 ms |
9720 KB |
Output is correct |
32 |
Correct |
11 ms |
9720 KB |
Output is correct |
33 |
Correct |
12 ms |
9720 KB |
Output is correct |
34 |
Correct |
13 ms |
9720 KB |
Output is correct |
35 |
Correct |
12 ms |
9720 KB |
Output is correct |
36 |
Correct |
11 ms |
9720 KB |
Output is correct |
37 |
Correct |
100 ms |
16504 KB |
Output is correct |
38 |
Correct |
51 ms |
14584 KB |
Output is correct |
39 |
Correct |
95 ms |
16504 KB |
Output is correct |
40 |
Correct |
230 ms |
18852 KB |
Output is correct |
41 |
Correct |
57 ms |
14892 KB |
Output is correct |
42 |
Correct |
154 ms |
18424 KB |
Output is correct |
43 |
Correct |
47 ms |
14456 KB |
Output is correct |
44 |
Correct |
80 ms |
15736 KB |
Output is correct |
45 |
Correct |
118 ms |
16120 KB |
Output is correct |