POJ 2186 强连通分量

题目:

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16768 Accepted: 6740

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.

Source

 

题意:

给N头奶牛 ,求被所有奶牛欢迎的奶牛的数量,欢迎关系为单向且可传递

 

代码:

//problem_id poj 2186

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define INF 0x1f1f1f1f
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

//first graph
int head[10005];
int point[50005];
int next[50005];

//reverse graph
int head_rev[10005];
int point_rev[50005];
int next_rev[50005];

//graph after find strong component
int head_new[10005];
int point_new[50005];
int next_new[50005];

//record the exit time of every in dfs
struct end_time
{
	int num;
	int exit_time;
}et[10005];

bool cmp(end_time e1,end_time e2)
{
	return e1.exit_time>e2.exit_time;
}

bool fl[10005];
int con_cnt[10005];//the point count of every component contain
int pt_in_com[10005];//which component of every point in

int e_time;//deep search over time
int new_node_cnt;//how many point of the new graph have
int new_edge_cnt;//how many edge of the new graph have
int out_dig[10005];//out digree of the new graph

void init()
{
	memset(head,-1,sizeof(head));
	memset(point,-1,sizeof(point));
	memset(next,-1,sizeof(next));

	memset(head_rev,-1,sizeof(head_rev));
	memset(point_rev,-1,sizeof(point_rev));
	memset(next_rev,-1,sizeof(next_rev));

	memset(head_new,-1,sizeof(head_new));
	memset(point_new,-1,sizeof(point_new));
	memset(next_new,-1,sizeof(next_new));

	memset(con_cnt,0,sizeof(con_cnt));
	memset(pt_in_com,0,sizeof(pt_in_com));
	memset(out_dig,0,sizeof(out_dig));

	e_time=0;
	new_node_cnt=0;
	new_edge_cnt=0;
}


void dfs(int v)
{
	fl[v]=1;
	for(int e=head[v];e!=-1;e=next[e])
	{
		if(!fl[point[e]])
		{
			dfs(point[e]);
		}
	}
	et[v].num=v;
	et[v].exit_time=e_time++;
}

void dfs_rev(int v)
{
	fl[v]=1;
	pt_in_com[v]=new_node_cnt;
	con_cnt[new_node_cnt]++;
	for(int e=head_rev[v];e!=-1;e=next_rev[e])
	{
		if(!fl[point_rev[e]])
		{
			dfs_rev(point_rev[e]);
		}
		else if(pt_in_com[point_rev[e]]!=0&&pt_in_com[point_rev[e]]!=new_node_cnt)
		{
			//add new edge and update out digree in new graph
			new_edge_cnt++;
			next_new[new_edge_cnt]=head_new[pt_in_com[point_rev[e]]];
			head_new[pt_in_com[point_rev[e]]]=new_edge_cnt;
			point_new[new_edge_cnt]=new_node_cnt;

			out_dig[pt_in_com[point_rev[e]]]++;
		}
	}
}

int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		init();

		for(int i=1;i<=m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);

			next[i]=head[a];
			head[a]=i;
			point[i]=b;

			next_rev[i]=head_rev[b];
			head_rev[b]=i;
			point_rev[i]=a;
		}

		memset(fl,0,sizeof(fl));
		for(int i=1;i<=n;i++)
		{
			if(!fl[i])
			{
				dfs(i);
			}
		}

		sort(et+1,et+n+1,cmp);

		memset(fl,0,sizeof(fl));
		for(int i=1;i<=n;i++)
		{
			if(!fl[et[i].num])
			{
				new_node_cnt++;
				dfs_rev(et[i].num);
			}
		}
		int res=0;
		for(int i=1;i<=new_node_cnt;i++)
		{
			if(out_dig[i]==0)
			{
				if(res!=0)
				{
					res=0;
					break;
				}
				res=i;
			}
		}
		if(res==0)
		{
			printf("0\n");
		}
		else
		{
			printf("%d\n",con_cnt[res]);
		}
	}
	return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值