poj Popular Cows 2186 (强连通分量 求有多少个点可以被 其他所有点到达) 好题

99 篇文章 0 订阅
8 篇文章 0 订阅
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27810 Accepted: 11205

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.
题意:
有n头牛,m个关系,每个关系有两个数(a,b) 表示a认为b更受欢迎。问n头牛中有多少头牛被   除它外所有的牛认为是受欢迎的。
思路:
求出所有SCC,和SCC里面的点以及对应的出度。
可分为三种情况考虑:
1、出度为0的SCC有0个,显然易见,该图就是一个强连通图,所有牛都满足条件;
2、出度为0的SCC有1个,满足条件的只有这一个SCC里面的牛;
3、出度为0的SCC超过1个,无论如何,出度超过0的SCC之间是无法连通的,所以没有牛满足
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<vector>
#define N 10010
#define M 60010
using namespace std;
int low[N];
int dfn[N],dfs_clock;
int sccon[N],scc_cnt;
bool instack[N];
vector<int>g[N];
vector<int>scc[N];
stack<int>s;
int n,m;
void tarjan(int u,int fa)
{
	int v;
	low[u]=dfn[u]=++dfs_clock;
	s.push(u);
	instack[u]=true;
	for(int i=0;i<g[u].size();i++)
	{
		v=g[u][i];
		if(!dfn[v])
		{
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v])
			low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u])
	{
		scc_cnt++;
		scc[scc_cnt].clear();
		while(1)
		{
			v=s.top();
			s.pop();
			instack[v]=false;
			sccon[v]=scc_cnt;
			scc[scc_cnt].push_back(v);
			if(v==u)
				break;
		}
	}
}
void find(int l,int r)
{
	memset(low,0,sizeof(low));
	memset(dfn,0,sizeof(dfn));
	memset(sccon,0,sizeof(sccon));
	memset(instack,false,sizeof(instack));
	scc_cnt=dfs_clock=0;
	for(int i=l;i<=r;i++)
		if(!dfn[i])
			tarjan(i,-1);
}
int in[N],out[N];
void suodian()
{
	int i,j;
	for(i=1;i<=scc_cnt;i++)
		in[i]=out[i]=0;
	for(i=1;i<=n;i++)
	{
		for(j=0;j<g[i].size();j++)
		{
			int u=sccon[i];
			int v=sccon[g[i][j]];
			if(u!=v)
			{
				in[v]++;
				out[u]++;
			}
		}
	}
}
void solve()
{
	find(1,n);
	suodian();
	int sum=0;//统计出度为0的SCC数目
	int ans=0;//统计最受欢迎的牛 
	for(int i=1;i<=scc_cnt;i++)
	{
		if(out[i]==0)//出度为0
		{
			sum++;
			ans+=scc[i].size();//累加
		}
		if(sum>1)//出度为0的SCC超过1个 不会有最受欢迎的牛
		{
			printf("0\n");
			return ;
		}
	}
	if(sum==0)//不存在出度为0的SCC 全部都是最受欢迎的牛
		printf("%d\n",n);
	else if(sum==1)//出度为0的SCC只有一个  该SCC里面的牛全是最受欢迎的 
		printf("%d\n",ans);
}
int main()
{
	int x,y;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
			g[i].clear();
		while(m--)
		{
			scanf("%d%d",&x,&y);
			g[x].push_back(y);
		}
		solve();
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值