Bobo has a directed acyclic graph with n points and m edges (that is, for any point v, there is no path from point v to point v).
For convenience, click 1, 2 , n number. Set count(x,y) to represent the number of paths from point x to point y (specify count(x,x)=0). Bobo wants to know
The remainder divided by (10 9 + 7).
Where a i,b j is a given sequence.
Input
The input contains no more than 15 sets of data.
The first row of each set of data contains two integers n,m (1 ≤ n,m ≤ 105)
Next, line i of line n contains two integers a i,b i (0 ≤ a i,b i ≤ 10 9)
The line i of the last m line contains two integers u i,v i, representing an edge (1 ≤ u i,v i ≤ n) from point u i to v i.
Output
For each set of data, an integer is output to represent the required value.
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
Train of thought:
It can be seen as a tree. It is easy to think of topology.
For each node, the successor node = (successor node + current node + b [current node]) is modeled again
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define pb push_back
#define IO ios::sync_with_stdio(false);
typedef long long ll;
const int N=1e5+5;
const double eps=1e-4;
const int MOD=1e9+7;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1}; ///Up, down, left and right
const int INF=0x3f3f3f3f;
struct Edge
{
int to,next;
Edge() {}
Edge(int to,int next):to(to),next(next) {}
} g[N];
int cnt=0;
int a[N],b[N],head[N];
int in[N];//Penetration
ll num[N]; //Explosive int
int n,m;
void addedge(int u,int v)
{
cnt++;
g[cnt]=Edge({v,head[u]});
head[u]=cnt;
in[v]++; /// penetration +1
}
void init()
{
cnt=0;
mem(head,-1);
mem(in,0);
mem(num,0);
}
void topo()///Topological sorting
{
queue<int>que;
for(int i=1; i<=n; i++)
{
if(!in[i])
{
que.push(i);
}
}
while(!que.empty())
{
int now=que.front();
que.pop();
///printf("now=%d\n",now);
for(int i=head[now]; ~i; i=g[i].next)
{
int to=g[i].to;
///printf("to=%d\n",to);
num[to]=(num[to]+num[now]+b[now])%MOD; ///Note that the intermediate result will explode int
in[to]--;
if(!in[to]) que.push(to);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1; i<=n; i++)
{
scanf("%d%d",&a[i],&b[i]);
}
for(int i=1; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(v,u);///The reverse construction is because the traversal of the chain forward star is opposite
}
/*
puts("");
for(int i=1;i<=n;i++)
{
printf("%d %d %d\n",g[i].to,g[i].next,head[i]);
}
puts("");
*/
topo();
ll ans=0;
for(int i=1; i<=n; i++)
{
ans=(ans+1LL*num[i]*a[i])%MOD;
}
printf("%lld\n",ans);
}
return 0;
}