# [HAOI2006][BZOJ1051] 受欢迎的牛

## 1051: [HAOI2006]受欢迎的牛

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2685  Solved: 1407
[Submit][Status][Discuss]

3 3
1 2
2 1
2 3

1

## HINT

100%的数据N<=10000,M<=50000

 970558 ws_fqk 1051 Accepted 3712 kb 96 ms C++/Edit 2371 B 2015-05-12 15:31:34 970484 ws_fqk 1051 Wrong_Answer 3468 kb 4 ms C++/Edit 2374 B 2015-05-12 14:48:33 970467 ws_fqk 1051 Runtime_Error 3476 kb 4 ms C++/Edit 2365 B 2015-05-12 14:35:52

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,tot,tot0,top,cnt,sum,ans,a,b;
bool inset[50001];
void insert(int a,int b)
{
tot++;
list[tot]=b;
}
void insert0(int a,int b)
{
list0[tot0]=b;
}
void dfs(int x)
{
dfn[x]=low[x]=++sum;
stack[++top]=x;
inset[x]=1;
while (y!=0)
{
if (!dfn[list[y]])
{
dfs(list[y]);
low[x]=min(low[x],low[list[y]]);
}
if (inset[list[y]]&&dfn[list[y]]<low[x]) low[x]=dfn[list[y]];
y=next[y];
}
y=-1;
if (dfn[x]==low[x])
{
cnt++;
while (y!=x)
{
y=stack[top--];
inset[y]=0;
belong[y]=cnt;
num[cnt]++;
}
}
}
void rebuild()
{
int y;
for (int i=1;i<=n;i++)
{
while (y!=0)
{
if (belong[i]!=belong[list[y]]) insert0(belong[i],belong[list[y]]);
y=next[y];
}
}
}
void tarjan()
{
for (int i=1;i<=n;i++) if (!dfn[i]) dfs(i);
}
int main()
{
tot=0; sum=0; top=0; cnt=0; tot0=0; ans=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(inset,0,sizeof(inset));
memset(belong,0,sizeof(belong));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
insert(a,b);
}
tarjan();
rebuild();
for (int i=1;i<=cnt;i++)
if (!head0[i]) {if (ans) {ans=0; break;} else ans=num[i];}
printf("%d\n",ans);
return 0;
}                  

posted @ 2015-05-12 15:29  ws_fqk  阅读(...)  评论(... 编辑 收藏