#include<cstring> #include<iostream> #include<cstring> #include<queue> #include<vector> #define maxn 100010 using namespace std; const long long mod = 1000000000 + 7; typedef long long ll; int de[maxn]; vector<int>G[maxn]; int list[maxn]; ll a[maxn]; ll b[maxn]; int t; void insert(int be, int en) { G[be].push_back(en); } ll ans = 0; queue<int>que; ll topu() { while (!que.empty()) { int x = que.front(); que.pop(); for (int i = 0; i < G[x].size(); i++) { int p = G[x][i]; ans = (ans + (a[x] * b[p]) % mod) % mod; a[p] = (a[p] + a[x]) % mod; de[p]--; if (de[p] == 0) { que.push(p); } } } return ans%mod; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld %lld", &a[i], &b[i]); } int be, en; for (int i = 0; i < m; i++) { scanf("%d %d", &be, &en); insert(be, en); de[en]++; } for (int i = 0; i < n; i++) { if (de[i] == 0) { que.push(i); } } ll a = topu(); printf("%lld\n", a%mod); return 0; }
原文:https://www.cnblogs.com/lesning/p/11632484.html