Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24548 | Accepted: 10072 |
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
Hint
Cow 3 is the only cow of high popularity.
Source
如果要受到所有牛的仰慕 则这个牛的点对于其他牛来说 都是可达的
首先对图进行缩点 得到DAG 如果在该DAG中只有一个叶子节点
则这个节点 都可以由其他节点到达 输出这个点所代表的联通分量中点的个数
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
void read(int &x)
{
char ch;
x=0;
while(ch=getchar(),ch!=' '&&ch!='\n')
{
x=x*10+ch-'0';
}
}
const int MAXN=10010;
const int MAXM=50010;
struct Edge
{
int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN];
int index,top;
int scc;
bool instack[MAXN];
int num[MAXN];
int n,m;
void addedge(int u,int v)
{
edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;
}
void Tarjan(int u)
{
// bug;
int v;
low[u]=dfn[u]=++index;
sta[top++]=u;
instack[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
// bug;
v=edge[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
// bug;
if(low[u]==dfn[u])
{
scc++;
do{
v=sta[--top];
instack[v]=0;
belong[v]=scc;
num[scc]++;
}while(u!=v);
}
}
int cd[MAXN];
void solve()
{
// bug;
MEM(dfn,0);
MEM(instack,0);
MEM(cd,0);
MEM(num,0);
index=scc=top=0;
for(int i=1;i<=n;i++)
if(!dfn[i])
Tarjan(i);
for(int u=1;u<=n;u++)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(belong[u]!=belong[v])
cd[belong[u]]++;
}
}
int cnt=0;
int x;
for(int i=1;i<=scc;i++)
{
if(cd[i]==0)
{
cnt++;
x=i;
}
}
if(cnt==1)
printf("%d\n",num[x]);
else puts("0");
}
void init()
{
tot=0;
MEM(head,-1);
}
int main()
{
// fread;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
int u,v;
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
solve();
}
return 0;
}