CSU 1804: A topological sort diagram of a direction-free graph

Source: Internet
Author: User

1804: A direction-free graph

Submit Page    Summary Time Limit: 5 Sec Memory Limit: Mb submitted: 716 Solved: 298

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
Hintsource Hunan Province The 12th session of the computer Program design competition to calculate each point I corresponding to the count (i,j) *BJ and then by AI, accumulation is the answer, note here to be similar to the topological sort, but to do backwards, to avoid the aftereffect of Reference blog HTTP/ blog.csdn.net/qq_21057881/article/details/52431139
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>using namespacestd;#defineLL Long LongConst intMAXN = 1e5+7;Const intMoD = 1e9+7; Vector<int>E[MAXN];intA[MAXN],B[MAXN],D[MAXN];intANS[MAXN];intMain () {intn,m;  while(Cin >> N >>m) {         for(intI=1; i<=n;i++) {e[i].clear (); } memset (d,0,sizeof(d)); memset (ans,0,sizeof(ans));  for(intI=1; i<=n;i++) {cin>> A[i] >>B[i]; }         for(intI=1; i<=m;i++){            intu,v; CIN>> u >>v;            E[v].push_back (U); D[u]++;//number of paths to the end of U} queue<int>Q;  for(intI=1; i<=n;i++){            if(D[i] = =0){//Join the queue with a point with a path number of 0 at the end of IQ.push (i); }        }         while(!Q.empty ()) {            intv =Q.front ();            Q.pop ();  for(intI=0; I<e[v].size (); i++){                intU =E[v][i]; Ans[u]= (Ans[u] + (Ans[v] + b[v])%mod)%MoD; //The reason is add b[v], is because the multiplication is equivalent to the whole, one is 1*b[v] equivalent to add b[v]d[u]--; if(D[u] = =0) {Q.push (U); }}} LL res=0;  for(intI=1; i<=n;i++) {res= (res + 1ll*ans[i]*a[i]%mod)%MoD; } cout<< Res <<Endl; }    return 0;}

CSU 1804: A topological sort diagram of a direction-free graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.