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;
}


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

求解有向图的强连通分量的SCC问题---POJ 2186 Popular Cows

【SCC问题】 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected),如果有向图G的每两个顶点都强连通,称G是一个强连通图.通俗的说法是:从图G内任意...

【强连通分量+缩点】 POJ 2186 Popular Cows

先求出强连通,再缩点。然后qiu

POJ2186 Popular Cows,Kosaraju算法+缩点

算法主要参考这篇解题报告点击打开链接,利用的是Kosaraju算法和缩点,给每个强连通分量染上颜色,缩成点后统计每个点的出度。 /****************************...
  • neofung
  • neofung
  • 2011年09月24日 01:37
  • 808

poj2186 Popular Cows--Kosaraju算法 & 缩点 & 强连通分量

原题链接:http://poj.org/problem?id=2186 题意:n头牛,标号1到n,m对关系,接下来m行,每行两个数字a和b代表牛a认为牛b受瞩目,当然如果同时牛b认为牛c也受...
  • LaoJiu_
  • LaoJiu_
  • 2016年09月09日 15:53
  • 227

POJ - 2186 Popular Cows(强连通分量+缩点)

上题: Every cow's dream is to become the most popular cow in the herd. In a herd of N (1
  • wjmwsgj
  • wjmwsgj
  • 2017年05月17日 11:01
  • 80

poj2186 Popular Cows(Tarjan+缩点水)

http://poj.org/problem?id=2186 题意:给你n头牛m个想法,(u,v)代表牛u认为v很受欢迎,欢迎度可以传递,求所有牛欢迎的牛有几只。 思路:直接求叶子节点...

文章标题 POJ 2186 : Popular Cows (强连通分量+缩点)

Popular CowsEvery cow’s dream is to become the most popular cow in the herd. In a herd of N (1

poj2186Popular Cows tarjan缩点

//n个奶牛, //A B 表示A认为B出名,而且其有传递性 //如A认为B出名,B认为C出名,那么A认为C出名 //问有多少头奶牛所有的奶牛都认为其出名 //先对这个图缩点,记录每一个缩点的个数,然...
  • cq_pf
  • cq_pf
  • 2015年08月09日 13:11
  • 439

poj 2186 Popular Cows(tarjan + 强连通分量 + 缩点)

http://poj.org/problem?id=2186 题意:有n头牛,m个膜拜关系,膜拜关系是不可逆的而且是单向传递的,比如A膜拜B,B膜拜C,那么A也膜拜C,但B不一定膜拜A。最后问有多少头...

(POJ 2186)Popular Cows 强连通分量 + 缩点图

Popular CowsTime Limit: 2000MS Memory Limit: 65536K Total Submissions: 33190 Accepted: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2186Popular Cows【scc+缩点】
举报原因:
原因补充:

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