tyvj1202 Number of food chains

Source: Internet
Author: User

Describe Tsyd learning the biological environment that one, the teacher left a job, is to give a food network, the total number of all food chains. (from the lowest trophic level creature (which cannot eat any other creature) begins to the highest trophic level (it cannot be eaten by any other creature) called a food chain)
Input to ensure that all living organisms have direct or indirect survival relationships. Input format the first line of N,M indicates that there are N (n<=50000) creatures, M (m<=100000) Eating relationships
The next M-line has two values, a, a, and a, B, respectively, to eat a (numbering starting from 1) output format Total number of food chains MOD 11129 value test Sample 1 input
3 3
1 2
2 3
1 3
Output
2
Note Example Explanation:
The two food chains were 1->3.
1->2->3
#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<vector>using namespacestd;Const intMAXN =100005, mod =11129; Vector<int>G[MAXN];intN,m,f[maxn],topo[maxn],cnt,ans;BOOLVIS[MAXN],IND[MAXN];voidinput () {scanf ("%d%d",&n,&m); intb;  for(inti =1; I <= m;i++) {scanf ("%d%d",&a,&b);        G[a].push_back (b); IND[B]=true; }}voidTopo_dfs (intx) {Vis[x]=true;  for(inti =0; i < g[x].size (); i++){        if(!Vis[g[x][i]] Topo_dfs (g[x][i]); } topo[cnt--] =x;}voidTopo_sort () {CNT=N;  for(inti =1; I <= n;i++){        if(!Vis[i]) Topo_dfs (i); }}voidDfsintx) {    if(!g[x].size ()) {F[x]=1; return; }     for(inti =0; i < g[x].size (); i++){        if(!F[g[x][i]]) DFS (g[x][i]); F[X]= (F[x] + f[g[x][i])%MoD; }}voiddp () { for(inti =1; I <= n;i++){        if(!F[topo[i]]) DFS (topo[i]); if(!ind[topo[i]]) ans = (ans + f[topo[i])%MoD; } cout<<ans;}intMain () {input ();    Topo_sort ();    DP (); return 0;}

tyvj1202 Number of food chains

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.