poj2186强联通+缩点

Popular Cows
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37945 Accepted: 15456

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.

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.

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.

#include<iostream>
#include<string.h>
#include<stdio.h>
#define maxn 10005
using namespace std;
int n,m;
int a,b;
struct node
{
int u;//保存每一条边的起始点
int v;//保存每一条边的终止点
int next;//保存每一条边的上一条边的编号
}edge[maxn*5];//保存每一条边，下标表示每一条边的编号
int dfn[maxn],low[maxn];
int stack[maxn];
int cnt,tot,top;
int num;
int sum[maxn];
int father[maxn];
int du[maxn];
bool check[maxn];
{
edge[k].u=u;
edge[k].v=v;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;
check[u]=true;
stack[++top]=u;
{
int v=edge[k].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(check[v])
{
low[u]=min(low[u],low[v]);
}
}
if(dfn[u]==low[u])
{
cnt++;
int temp;
do
{
temp=stack[top--];
father[temp]=cnt;
sum[cnt]++;
check[temp]=false;
}while(u!=temp);
}
}
void solve()
{
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
}
void init()
{
num=0;
tot=0;
cnt=0;
top=0;
memset(check,false,sizeof(check));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(father,0,sizeof(father));
memset(sum,0,sizeof(sum));
memset(du,0,sizeof(du));
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
}
solve();
for(int i=1;i<=n;i++)
{
{
if(father[i]!=father[edge[j].v])
{
du[father[i]]++;
}
}
}
int t=0,ans;
for(int i=1;i<=cnt;i++)
{
if(!du[i])
{
t++;
ans=sum[i];
}
}
if(t==1)
{
printf("%d\n",ans);
}
else
{
printf("0\n");
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120