poj2186Popular Cows【scc+缩点】

原创 2015年11月19日 11:45:08

Language:
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27987   Accepted: 11275

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


题意:没头牛都有认为一头牛最知名具有传递性求被所有牛都认为知名的牛的个数

解题思路:scc+缩点如果为强连通图数目为n缩点后出度大于1则不存在;出度为一时输出出度为1的scc的牛数即可

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f3f
using namespace std;
const int maxn=100010;
int dfs_clock,scc_cnt;
int head[maxn];
bool instack[maxn];
int low[maxn],dfn[maxn];
int sccno[maxn];
int in[maxn],out[maxn];
stack<int>S;
vector<int>G[maxn];
vector<int>scc[maxn];
struct Node{
	int from,to,next;
}A[maxn];
void init(){
	dfs_clock=scc_cnt=0;
	memset(head,-1,sizeof(head));
	memset(low,0,sizeof(low));
	memset(dfn,0,sizeof(dfn));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(sccno,0,sizeof(sccno));
	memset(instack,false,sizeof(instack));
}
int MIN(int a,int b){
	return a<b?a:b;
}
void tarjan(int u,int pre){
	low[u]=dfn[u]=++dfs_clock;
	S.push(u);instack[u]=true;
	int v;
	for(int k=head[u];k!=-1;k=A[k].next){
		v=A[k].to;
		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++;
		G[scc_cnt].clear();
		scc[scc_cnt].clear();
		while(1){
			v=S.top();S.pop();
			instack[v]=false;
			sccno[v]=scc_cnt;
			scc[scc_cnt].push_back(v);
			if(u==v)break;
		}
	}
}
void suodian(int m){
	for(int i=0;i<m;++i){
		int u=sccno[A[i].from];
		int v=sccno[A[i].to];
		if(u!=v){
			G[u].push_back(v);
			in[v]++;out[u]++;
		}
	}
}
int main()
{
	int n,m,i,j,k;
	while(scanf("%d%d",&n,&m)!=EOF){
		init();
		for(i=0;i<m;++i){
			scanf("%d%d",&A[i].from,&A[i].to);
			A[i].next=head[A[i].from];
			head[A[i].from]=i;
		}
		for(i=1;i<=n;++i){
			if(!dfn[i])tarjan(i,-1);
		}
		suodian(m);
		int ans=0,pos;
		for(i=1;i<=scc_cnt;++i){
			if(!out[i]){
				ans++;
				pos=i;
			}
		}
		if(scc_cnt==1){
			printf("%d\n",n);
		}
		else if(ans>1){
			printf("%d\n",0);
		}
		else printf("%d\n",scc[pos].size());
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ - 2186 - Popular Cows (tarjan)

Popular Cows题目传送:Popular Cows思路:tarjan算法求强连通分量AC代码:#include #include #include #include #include ...

poj2186 - Popular Cows

想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410              ...

P2746 [USACO5.3]校园网Network of Schools

题目链接 https://www.luogu.org/problem/show?pid=2746#include #include #include #include #define N 205 us...

POJ2186-Popular Cows

转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6764104   大致题意: 有N只奶牛,其中奶牛A认为奶牛B备受...

洛谷 P3110 [USACO14DEC]驮运Piggy Back spfa

题目链接 https://www.luogu.org/problem/show?pid=3110 题目描述Bessie and her sister Elsie graze in different...

POJ2186 Popular Cows【Tarjan】【强连通分量】

题目大意:每头奶牛都希望自己成为最欢迎的那头牛。给你N头牛,M个崇拜关系(A,B)。 意思是牛A崇拜牛B。特别是,如果牛A崇拜牛B,牛B崇拜牛C,那么牛A也崇拜牛C。那么 问题来了:请计算出被所有牛崇...

POJ2186 Popular Cows Tarjan 链接表 pascal解题报告

2186是我OI史上第一道Tarjan,之前看了很多的题解都是C或者C++的,让我一个学pascal的很难堪,做了一天之后AC,便想到写一篇pascal的解题报告。 题目的大意就是说有若干只奶牛,奶...

poj2186

Tarjan模板题。建议采用二维vector存储。 学习Tarjan时要注意两个数组: int DFN[M]; //深度优先搜索访问次序 int Low[M];...

洛谷 P2341 [HAOI2006]受欢迎的牛

题目链接https://www.luogu.org/problem/show?pid=2341 #include #include #include #include #define N 5...

POJ 2186 Popular Cows

首先声明: 这个题目的后台数据很水,有很多数据都没有,一些错误的方法也可能会AC。 题目大意: 有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。给你M个关系(谁认为谁牛),求大家都认为它很...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)