一个数,即有多少头牛被所有的牛认为是受欢迎的。
没有什么好说的
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int N = 10005; struct Edge{ int to,next; }e[N*5],ee[N*5]; int dfn[N],low[N],m,n; int id,scnt,cnt; int last[N],last2[N],q[N],top,belong[N],to[N],ans=0; bool inq[N]; void insert( int u, int v ){ e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt; } void insert2( int u, int v ){ ee[++cnt].to = v; ee[cnt].next = last2[u]; last2[u] = cnt; } void tarjan( int x ){ int now = 0; dfn[x] = low[x] = ++id; q[++top] = x; inq[x] = 1; for( int i = last[x]; i; i = e[i].next ) if(!dfn[e[i].to]){ tarjan(e[i].to); low[x] = min(low[x],low[e[i].to]); } else if( inq[e[i].to] ) low[x] = min(low[x],dfn[e[i].to]); if( low[x] == dfn[x] ){ scnt++; while( now != x ){ now = q[top--]; inq[now] = 0; belong[now] = scnt; ++to[scnt]; } } } void rebuild(){ cnt = 0; for( int i = 1; i <= n; i++ ) for( int j = last[i]; j; j = e[j].next ) if( belong[e[j].to] != belong[i] ) insert2(belong[i],belong[e[j].to]); } int main(){ scanf("%d%d", &n, &m); for( int i = 1,u,v; i <= m; i++ ){ scanf("%d%d", &u, &v); insert(u,v); } for( int i = 1; i <= n; i++ ) if( !dfn[i] ) tarjan(i); rebuild(); for( int i = 1; i <= scnt; i++ ) if( !last2[i] ){ if( ans ){ans = 0; break;} else ans = to[i]; } printf("%d", ans); return 0; }HOME Back