1804: Forward loop-free graph time limit:5 Sec Memory limit:128 MB
submit:341 solved:152
[Submit] [Status] [Web Board] The Descriptionbobo has an n point, a forward-free graph of M-bars (i.e., for any point V, there is no path from point V to end of Point v). For convenience, click,..., N number. Set count (x, y) to indicate the number of paths (specified by count (x,x) =0) for points x to Y, Bobo want to know the remainder of the divide (109+7). Where AI,BJ is a given number of sequences. Input inputs contain no more than 15 sets of data. The first row of each group of data contains two integers n,m (1≤n,m≤105). The next line of n lines contains two integers, Ai,bi (0≤ai,bi≤109). The last M line contains two integers ui,vi, representing an edge from the point UI to the VI (1≤UI,VI ≤n). Output for each set of data, outputs an integer that represents the required value. Sample Input
3 31 11 11 11 21 32 32 21 00 21 21 22 1500000000 00 5000000001 2
Sample Output
44250000014
Idea: topological sort +dp;
It is possible to merge the points by the topology.
1#include <stdio.h>2#include <algorithm>3#include <string.h>4#include <iostream>5#include <math.h>6#include <queue>7#include <vector>8 using namespacestd;9typedefLong LongLL;TenLL mod = 1e9+7; OneLL dp[100005]; A intcnt[100005]; -LL a[100005]; -LL b[100005]; thevector<int>vec[100005]; -queue<int>que; - intMainvoid) - { + intn,m; - while(SCANF ("%d%d", &n,&m)! =EOF) + { A inti,j; LL ask; at while(!que.empty ()) - Que.pop (); - for(i =0; I <100005; i++) - vec[i].clear (); -memset (CNT,0,sizeof(CNT)); -Memset (DP,0,sizeof(DP)); in for(i =1; I <= N; i++) - { toscanf"%lld%lld",&a[i],&b[i]); + } - while(m--) the { * intx, y; $scanf"%d%d",&x,&y);Panax Notoginsengcnt[y]++; - vec[x].push_back (y); the } + for(i =1; I <= N; i++) A { the if(Cnt[i] = =0) + { - Que.push (i); $ } $ } - while(!que.empty ()) - { the intx =Que.front (); - Que.pop ();Wuyi for(i =0; i < vec[x].size (); i++) the { - inty =Vec[x][i]; WuCnt[y]--; - if(Cnt[y] = =0) About Que.push (y); $Dp[y] = dp[y]+a[x]*b[y]%MoD; -dp[y]%=MoD; -A[y] = A[y] +A[x]; -a[y]%=MoD; AAsk =Dp[y]; + } the}ask =0; - for(i =1; I <= n;i++) $ { theAsk = Ask +Dp[i]; theAsk%=MoD; the } theprintf"%lld\n", ask); - } in return 0; the}
1804: A direction-free graph