B - Directed Acyclic Graph CSU - 1804

Bobo has an n-point, m-edge directed acyclic graph (that is, for any point v, there is no path that starts at v and ends at v).
For convenience, the points are numbered 1,2,…,n. Let count(x,y) denote the number of different paths from point x to point y (specifying count(x,x)=0), Bobo wants to know
Remainder of division by (10 9 +7).
Among them, a i , b j is the 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≤10 5 ).
The ith line of the next n lines contains two integers a i ,b i (0≤a i ,bi ≤10 9 ) .
The ith row of the last m rows contains two integers ui , v i , representing an edge from point ui to v i (1≤u i ,v i ≤n ).
Output
For each set of data, output an integer representing the desired 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
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;

const int maxn=1e5+10;
const int mod=1e9+7;

int india[maxn];
ll a[maxn],b[maxn];

vector<int>ve[maxn];
int n,m;
/*
This question eavesdropped on other people's ideas, topological sorting, and then knocked on it. I don't know if my teammates are sick, but my mother made the input wrong. I'm really speechless.
Obviously the back is right, the input is wrong, how to break. . . . . , very uncomfortable

This question also has a bit of dp thinking, that is, we don't want to calculate it individually, we calculate it together, and it can be pushed directly online, and finally we can do it.
*/

intmain()
{
	ios::sync_with_stdio(false);
	while(cin>>n>>m){
		for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	
		memset(inde,0,sizeof(inde));
		for(int i=1;i<=n;i++) ve[i].clear();
	
		int u, v;
		for(int i=1;i<=m;i++)
		{
			cin>>u>>v;
			inde [v] ++;
			ve[u].push_back(v);
		}
	
		queue<int>q;
		for(int i=1;i<=n;i++){
			if(inde[i]==0){
				q.push(i);
			}
		}
		ll ans=0;
		while(!q.empty()){
			u=q.front(); q.pop();
			//printf("u : %d c : %lld\n",u,aa);
			int sz=ve[u].size();
			for(int i=0;i<sz;i++){
				v=ve[u][i];
				ans=(ans+a[u]*b[v]%mod)%mod;
				a[v]=(a[u]+a[v])%mod;
				inde [v] -;
				if(inde[v]==0){  
					q.push(v);
				}
	 	   }
		}
		cout<<ans<<endl;
    }
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823295&siteId=291194637