poj 2186 Popular Cows(强连通缩点)

25 篇文章 0 订阅
8 篇文章 0 订阅
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23392 Accepted: 9580

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.
/*
是一道模板题,第一天敲出来,隔天又敲的。强连通第一道题,还是没有独立调试出来,看着人家的博客对出来的
加油!!!
2014-08-16 19:13
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=10010;
struct Edge{
	int to,next;
}edge[MAX*5];
int N,M;
int edgeNum,head[MAX];
int dfn[MAX],low[MAX],index;
int stack[MAX],Top;
int belong[MAX],cnt,out[MAX];
bool instack[MAX];
void AddEdge(int a,int b){
	edge[edgeNum].to=b;
	edge[edgeNum].next=head[a];
	head[a]=edgeNum++;
}
void Init(){
	index=cnt=edgeNum=0;
	memset(instack,0,sizeof(instack));
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(out,0,sizeof(out));
	memset(stack,0,sizeof(stack));
	memset(belong,0,sizeof(belong));
		int a,b;
		for(int i=1;i<=M;i++){
			scanf("%d%d",&a,&b);
			AddEdge(a,b);
		}
}
void Tarjan(int u){
	low[u]=dfn[u]=++index;
	stack[++Top]=u;
	instack[u]=true;
	int v;
	for(int i=head[u];~i;i=edge[i].next){
		int v=edge[i].to;
		if(dfn[v]==0){
			Tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v]){
			low[u]=min(low[u],dfn[v]);
		}	
	}
	if(dfn[u]==low[u]){
		cnt++;
		do{
			v=stack[Top--];
			belong[v]=cnt;
			instack[v]=false;
		}while(u!=v&&Top!=0);
	}
	
}
void solve(){

	while(scanf("%d%d",&N,&M)!=EOF){//不能在初始化中输入,切记 
			Init();
		for(int i=1;i<=N;i++){
			if(!dfn[i]){
			Tarjan(i);
			}
		}
		int u;
		for(int i=1;i<=N;i++){
			for(int j=head[i];~j;j=edge[j].next){
				u=edge[j].to;
				if(belong[i]!=belong[u]){
					out[belong[i]]++;//belong[i]不会超过cnt 
				}
			}
		}
		int num=0,ans=0;
		int temp;//记录缩点后的次序 
		for(int i=1;i<=cnt;i++){
			if(out[i]==0){
				num++;
				temp=i;
				}
		}
		if(num==1){
			for(int i=1;i<=N;i++){
				if(belong[i]==temp)//belong[i]应该是与temp一样 
				ans++;
			}
			printf("%d\n",ans);
		}
		else{
			printf("0\n");
		}
	}
}
int main(){
	solve();
return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值