Topic background
This problem has been fixed test data.
Title Description
Each cow dreams of becoming a star in the bullpen. Like all cows are cows cow is a star. All milk
Cattle are narcissistic per cow always like to own. Between cows "like" can be delivered - if A hi
Huan B, B like C, then A also like C. A total of N cowshed cows given love relationship between the number of cows, you
Calculate how many cows can become a star.
Input Format
first line: space-separated by two integers: N and M
second row to row M + 1: an integer of two space-separated each line: A and B, and A represent like B
Output Format
The first line: a single integer representing the number of stars cows
Sample input and output
3 3 1 2 2 1 2 3
1
Description / Tips
Only 3 cows can be a star
【data range】
10% of the data N <= 20, M <= 50
30% of the data N <= 1000, M <= 20000
70% of the data N <= 5000, M <= 50000
100% data N <= 10000, M <= 50000
Problem-solving ideas: the title is meant to find all the cattle like cattle to reduce the number of points and then find the point of zero degrees
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <vector> 5 #include <algorithm> 6 #include <stack> 7 using namespace std; 8 int n,m,tot=0,cnt; 9 const int maxn=5e4+5; 10 vector<int> G[maxn]; 11 stack<int> sta; 12 int dfn[maxn],low[maxn],vis[maxn],save[maxn],dis[maxn],out[maxn],x[maxn],y[maxn]; 13 14 void tarjan(int ee){ 15 dfn[ee]=low[ee]=++tot; 16 vis[ee]=1;sta.push(ee); 17 for(int i=0;i<G[ee].size();i++){ 18 int u=G[ee][i]; 19 if(dfn[u]==0){ 20 tarjan(u); 21 low[ee]=min(low[ee],low[u]); 22 }else if(vis[u]){ 23 low[ee]=min (Low [EE], DFN [U]); 24 } 25 } 26 is IF (DFN [EE] == Low [EE]) { 27 CNT ++ ; 28 the while (! sta.empty ()) { 29 int Top = sta.top (); 30 sta.pop (); 31 is VIS [Top] = 0 ; 32 DIS [CNT] ++; /// this strongly connected components inside how many values 33 is Save [Top] = CNT; / // this node which belongs to the strongly connected component 34 is IF (Top == EE) BREAK ; 35 } 36 } 37 } 38 39 int main(){ 40 scanf("%d%d",&n,&m); 41 for(int i=1;i<=m;i++){ 42 scanf("%d%d",&x[i],&y[i]); 43 G[x[i]].push_back(y[i]); 44 } 45 for(int i=1;i<=n;i++){ 46 if(dfn[i]==0) tarjan(i); 47 } 48 for ( int I = . 1 ; I <= m; I ++ ) { 49 IF (Save [X [I]] = Save [Y [I]]!) OUT [Save [X [I]]] ++; // / the strongly connected components of a number of 50 } 51 is int in Flag = 0 , RES; 52 is for ( int I = . 1 ; I <= cnt; I ++) { /// number cnt strongly connected component 53 is IF ( oUT [ I] == 0 ) { 54 is In Flag ++; RES = DIS [I]; 55 } 56 is } 57 is the printf ( "%d\n",flag==1?res:0); 58 return 0; 59 }