Problem Description
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Line 1: Two space-separated integers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
***************************************************************************************************************************tarjan 求强联通分量,再求出度为0的点,
***************************************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 int dfn[10011],low[10011],vec[100011],nxt[1110001],id[10011],sta[1000111],head[11001],vis[11001],num[11001],in[11001]; 8 int e_num,ans,I,n,m,s,t,k,re; 9 void init() 10 { 11 e_num=0;ans=0,I=0,re=0; 12 memset(dfn,0,sizeof(dfn)); 13 memset(low,0,sizeof(low)); 14 memset(vec,0,sizeof(vec)); 15 memset(id,0,sizeof(id)); 16 memset(head,-1,sizeof(head)); 17 memset(sta,0,sizeof(sta)); 18 memset(vis,0,sizeof(vis)); 19 memset(num,0,sizeof(num)); 20 memset(in,0,sizeof(in)); 21 } 22 void add(int u,int v) 23 { 24 vec[I]=v; 25 nxt[I]=head[u]; 26 head[u]=I++; 27 } 28 void tarjan(int x)//tarjan缩点 29 { 30 dfn[x]=low[x]=++e_num; 31 sta[++ans]=x; 32 vis[x]=1; 33 for(int it=head[x];it!=-1;it=nxt[it]) 34 { 35 int cur=vec[it]; 36 if(!dfn[cur]) 37 { 38 tarjan(cur); 39 low[x]=min(low[cur],low[x]); 40 } 41 else if(!id[cur]) 42 { 43 low[x]=min(low[x],dfn[cur]); 44 } 45 } 46 if(dfn[x]==low[x]) 47 { 48 re++; 49 int v; 50 do 51 { 52 v=sta[ans--]; 53 num[re]++; 54 vis[v]=0; 55 id[v]=re; 56 }while(v!=x); 57 } 58 } 59 int main() 60 { 61 while(scanf("%d%d",&n,&m)!=EOF) 62 { 63 init(); 64 for(int i=1;i<=m;i++) 65 { 66 scanf("%d %d",&s,&t); 67 add(s,t); 68 } 69 for(int i=1;i<=n;i++) 70 if(!dfn[i]) 71 { 72 tarjan(i); 73 } 74 for(int i=1;i<=n;i++) 75 for(int j=head[i];j!=-1;j=nxt[j]) 76 if(id[vec[j]]!=id[i]) 77 in[id[i]]++; 78 ans=0; 79 for(int i=1;i<=re;i++) 80 if(in[i]==0) 81 { 82 //sta[ans++]=i; 83 ans++; 84 k=i; 85 86 } 87 if(ans>1)printf("0 ");//多个连通块,不满足 88 else 89 printf("%d ",num[k]); 90 91 } 92 return 0; 93 }
弄了一上午!!!