思路:先将每个点i对应的count(i,j)*bj算出来然后乘ai,累加就是答案,注意这里要类似拓扑排序那样,不过要倒着做,避免后效性
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1e5+7;
const int mod = 1e9+7;
vector<int>e[maxn];
LL a[maxn],b[maxn],d[maxn];
LL ans[maxn];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 0;i<=n;i++)
e[i].clear();
memset(d,0,sizeof(d));
memset(ans,0,sizeof(ans));
for(int i = 1;i<=n;i++)
scanf("%lld%lld",&a[i],&b[i]);
for(int i = 1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[v].push_back(u);
d[u]++;
}
queue<int>q;
for(int i = 1;i<=n;i++)
if(d[i]==0)q.push(i);
while(!q.empty())
{
int u = q.front();q.pop();
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i];
ans[v]=(ans[v]+(ans[u]+b[u])%mod)%mod;
if(--d[v]==0)
q.push(v);
}
}
LL res = 0;
for(int i = 1;i<=n;i++)
res = (res + 1LL*ans[i]*a[i]%mod)%mod;
printf("%lld\n",res);
}
}
Description
Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始、点 v 结束的路径)。
为了方便,点用 1,2,…,n 编号。 设 count(x,y) 表示点 x 到点 y 不同的路径数量(规定 count(x,x)=0),Bobo 想知道

除以 (109+7) 的余数。
其中,ai,bj 是给定的数列。
Input
输入包含不超过 15 组数据。
每组数据的第一行包含两个整数 n,m (1≤n,m≤105).
接下来 n 行的第 i 行包含两个整数 ai,bi (0≤ai,bi≤109).
最后 m 行的第 i 行包含两个整数 ui,vi,代表一条从点
ui 到 vi 的边 (1≤ui,vi≤n)。
Output
对于每组数据,输出一个整数表示要求的值。
Sample Input
3 3
1 1
1 1
1 1
1 2
1 3
2 3
2 2
1 0
0 2
1 2
1 2
2 1
500000000 0
0 500000000
1 2
Sample Output
4
4
250000014