[强连通分量] POJ 2186 Popular Cows

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31815   Accepted: 12927

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头牛,m对形式(A,B)代表A觉得B很厉害,特殊的,如果A觉得B很厉害,B觉得C很厉害,那么A觉得C也很厉害,问有多少头牛被所有牛(除了它自己)觉得很厉害。
 
本题比较考验基础,涉及到链表,强连通分量的有关知识,对初学图论者还是有好处的。
 
解题思路:设n头牛中两两之间都觉得对方很厉害为一堆牛,而其中有一头牛被所有牛都认为很厉害,那么很显然,这堆牛都被所有牛认为很厉害。
              那会不会有两堆以上的牛都被所有人认为很厉害呢?显然不会,如果两堆牛都被所有人认为很厉害,那么这两堆牛两两之间一定都觉得自己很厉害,其实还是一堆牛。
              那么,这就可以演化为一个图,有n个点,m条有向边,求唯一的一个出度为0的强联通分量中元素的个数。
              为什么唯一呢?
              显然,如果有两个出度为0的强连通分量,那么这两个强连通分量都不会觉得对方分量的牛很厉害,而原题是找被所有牛都觉得很厉害,于是此时没有牛被所有牛认为很厉害。
             我们用链表存储点的关系,用tarjian找强连通分量,最后用强连通分量的出度判断是否是我们找的那一群牛,统计牛的个数就可以了。
上代码:
    
#include<stdio.h>
#include<string.h>
struct list
  {
  	 int v;
  	 list *next;
  };
list *head[10010],*rear[10010];
int dfn[10010],low[10010],stack[10010],s[10010],top,times,cnt;
bool instack[10010],chu[10010];
void tarjian(int v)
  {
  	 dfn[v]=low[v]=++times;
  	 instack[v]=true;
  	 stack[top++]=v;
  	 for(list *p=head[v];p!=NULL;p=p->next)
  	   if(!dfn[p->v])
	   {
  	   	  tarjian(p->v);
  	   	  if(low[p->v]<low[v]) low[v]=low[p->v];
       }else if(instack[p->v]&&low[p->v]<low[v]) low[v]=low[p->v];
     if(low[v]==dfn[v])
       {
       	  ++cnt;
       	  do
       	  {
       	  	v=stack[--top];
       	  	instack[v]=false;
       	  	s[v]=cnt;
		  }while(low[v]!=dfn[v]);
	   }
	 return;
  }
int main()
  {
  	 int n,m,i,begin,to,a,b;
  	 scanf("%d%d",&n,&m);
  	 memset(rear,0,sizeof(rear));
  	 memset(head,0,sizeof(head));
  	 for(i=0;i<m;++i)
  	   {
  	   	  scanf("%d%d",&begin,&to);
  	   	  if(rear[begin]!=NULL)
  	   	    {
  	   	    	rear[begin]->next=new list;
  	   	    	rear[begin]=rear[begin]->next;
		    }else head[begin]=rear[begin]=new list;
		  rear[begin]->next=NULL;
		  rear[begin]->v=to;
	   }
	 memset(dfn,0,sizeof(dfn));
	 memset(low,0,sizeof(low));
	 memset(stack,0,sizeof(stack));
	 memset(s,0,sizeof(s));
	 times=cnt=top=0;
	 for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i); 
	 if(cnt==1) 
	   {
	   	  printf("%d
",n);
	   	  return 0;
	   }
	 memset(chu,false,sizeof(chu));
	 for(i=1;i<=n;++i) 
	    for(list *p=head[i];p!=NULL;p=p->next)
	      	if(s[i]!=s[p->v])
	      	  chu[s[i]]=true;
	  a=0;
	  for(i=1;i<=cnt;++i) if(chu[i]) ++a;else b=i;
	  if(a==cnt-1)
	     {
	       a=0;
	       for(i=1;i<=n;++i) if(s[i]==b) ++a;
	       printf("%d
",a);
		 }else printf("0
");
  	 return 0;
  }

  

   
原文地址:https://www.cnblogs.com/fuermowei-sw/p/6139556.html