Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define inf 2000000001 int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } struct is { int u,v; int next; }edge[50010]; int head[50010]; int belong[50010]; int dfn[50010]; int low[50010]; int stackk[50010]; int instack[50010]; int number[50010]; int du[100010]; int n,m,jiedge,lu,bel,top; void update(int u,int v) { jiedge++; edge[jiedge].u=u; edge[jiedge].v=v; edge[jiedge].next=head[u]; head[u]=jiedge; } void dfs(int x) { dfn[x]=low[x]=++lu; stackk[++top]=x; instack[x]=1; for(int i=head[x];i;i=edge[i].next) { if(!dfn[edge[i].v]) { dfs(edge[i].v); low[x]=min(low[x],low[edge[i].v]); } else if(instack[edge[i].v]) low[x]=min(low[x],dfn[edge[i].v]); } if(low[x]==dfn[x]) { int sum=0; bel++; int ne; do { sum++; ne=stackk[top--]; belong[ne]=bel; instack[ne]=0; }while(x!=ne); number[bel]=sum; } } void tarjan() { memset(dfn,0,sizeof(dfn)); bel=lu=top=0; for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i); } int main() { int i,t; while(~scanf("%d%d",&n,&m)) { memset(head,0,sizeof(head)); jiedge=0; for(i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); update(u,v); } tarjan(); for(i=1;i<=jiedge;i++) if(belong[edge[i].v]!=belong[edge[i].u]) du[belong[edge[i].u]]++; int flag=0,pos; /*for(i=1;i<=n;i++) cout<<belong[i]<<endl;*/ for(i=1;i<=bel;i++) { if(!du[i]) { flag++; pos=i; } } if(flag!=1) printf("0\n"); else printf("%d\n",number[pos]); } return 0; }