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