Title Description
Is a schematic view of a food web ecosystem, according to FIG Answer first small chain shown in FIG. Now you question and n and m species energy flow relationship, which find food.
Species name is numbered from 1 to n relationship between the M-shaped flow of energy such as a1 b1a2 b2a3 b3 ...... am-1 bm-1am bm where ai ai BI represents the energy flow from the species
Species bi, an isolated individual attention is not a biological food chain.
Input Format
The first line of two integers n and m, each row next two rows ai bi integers m and m describe the relationship between the flow of energy. (Data ensure biological characteristics of input data symbols, and will not
Duplicate energy flow relations) 1 <= N <= 100000 0 <= m <= 200000 Title answer guaranteed not burst int
Output Format
An integer that is the number of food chains in the food web
Problem solution: dp DAG entry on the subject, most of the problem solution is memory that is a version of the search to find a food chain to find out a degree of zero point, to find a great
The food chain, I wrote qbxt zhhx Gangster write an array of recursive version, with a queue to maintain it, because it is DAG, so the number of food chains each point it is all pre-nodes
After a number of food chains, no processing time, the degree minus one, when the zero-degree, has been on behalf of the transfer is complete, the final count is zero for all the food chain that is the answer to the last point.
Turntable transfer equation:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #define maxn 100005 7 8 using namespace std; 9 10 struct node 11 { 12 int ed,nxt; 13 }; 14 node edge[200005]; 15 int ind[maxn],first[maxn],dp[maxn],n,m,cnt; 16 int out[maxn]; 17 18 inline void add_edge(int s,int e) 19 { 20 cnt++; 21 edge[cnt].ed=e; 22 edge[cnt].nxt=first[s]; 23 first[s]=cnt; 24 return; 25 } 26 27 queue <int> q; 28 29 int main() 30 { 31 scanf("%d%d",&n,&m); 32 for(int i=1;i<=m;i++) 33 { 34 int s,e; 35 scanf("%d%d",&s,&e); 36 add_edge(s,e); 37 ind[e]++; 38 out[s]++; 39 } 40 for(int i=1;i<=n;i++) 41 if(ind[i]==0) 42 { 43 if(out[i]) dp[i]=1; 44 q.push(i); 45 } 46 while(!q.empty()) 47 { 48 int p=q.front(); q.pop(); 49 for(int i=first[p];i;i=edge[i].nxt) 50 { 51 int e=edge[i].ed; 52 dp[e]+=dp[p]; ind[e]--; 53 if(ind[e]==0) q.push(e); 54 } 55 } 56 int ans=0; 57 for(int i=1;i<=n;i++) 58 if(out[i]==0) ans+=dp[i]; 59 printf("%d",ans); 60 return 0; 61 }