poj 2186 强连通分量

POJ 2186 Popular Cows(强连通分量)

Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13835 Accepted: 5484

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.

分析:找出最受欢迎的牛,那么这个牛肯定是老大,他不会欢迎其他牛,也就是说它的出度肯定是0,而且是唯一一个


#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXM = 50010*2;
const int MAXV = 10010;
struct node
{
	int  to, next;
}edge[MAXM];
int head[MAXM];
int  stop, cnt, vis, dfn[MAXV], low[MAXV];
bool instack[MAXV];
int stack[MAXV], belong[MAXV];
bool MostPopular[MAXV];
int M, N;
void tarjan(int i)
{
    int j;
    dfn[i]=low[i]=++vis;
    instack[i]=true;
    stack[++stop]=i;
    for(int k=head[i];k!=-1;k=edge[k].next){
         j=edge[k].to;
        if(!dfn[j]){
            tarjan(j);
            low[i]=min(low[i],low[j]);
        }
        else if(instack[j])
            low[i]=min(low[i],dfn[j]);
    }
    if(dfn[i]==low[i]){
        do{
            j=stack[stop--];
            instack[j]=false;
            belong[j]=cnt;
        }while(j!=i);
        cnt++;
    }
}
void solve()
{
	stop = cnt = vis = 0;
	memset(dfn,0,sizeof(dfn));
	for(int i=1;i<=N;i++)
        if(!dfn[i])
        tarjan(i);
    for(int i=0;i<cnt;i++)
        MostPopular[i]=true;
    for(int i=1;i<=N;i++)
        for(int j=head[i];j!=-1;j=edge[j].next)
        if(belong[i]!=belong[edge[j].to]){
        MostPopular[belong[i]]=false;
        break;
        }
    int num=0;int topo=0;
    for(int i=0;i<cnt;i++)
    if(MostPopular[i]==true){
        num++;topo=i;
    }
    int ans=0;
    if(num==1){
        for(int i=1;i<=N;i++)
            if(belong[i]==topo)
            ans++;
        cout<<ans<<endl;
    }
    else cout<<0<<endl;
}
int main()
{
	while (scanf("%d%d", &N, &M) != EOF){

		memset(head, -1, sizeof(head));
		for (int i = 0; i < M; i++){
			int a, b;
			cin >> a >> b;
			edge[i].next = head[a];
			edge[i].to = b;
			head[a] = i;
		}
		solve();
	}
	return 0;
}

借鉴的代码:

#include<iostream>
using namespace std;

struct edge
{
 int to;
 int next;
}e[100001];
int head[10001];
int e_num;

const int MAX = 10001;

int Stop;//栈中的元素个数
int cnt;//记录连通分量的个数
int visitNum;//记录遍历的步数
int dfn[MAX];//记录节点u第一次被访问时的步数
int low[MAX];//记录与节点u和u的子树节点中最早的步数
bool instack[MAX];//记录节点u是否在栈中
int stack[MAX];//栈
int Belong[MAX];//记录每个节点属于的强连通分量编号

int N;//节点个数
bool isMostPopular[MAX];

void add(int a,int b)
{
 e[e_num].next = head[a];
 e[e_num].to = b;
 head[a] = e_num++;
}
void tarjan(int i)
{
 int j;
 dfn[i] = low[i] = ++visitNum;
 instack[i] = true;
 stack[++Stop] = i;
 for(unsigned k=head[i];k!=-1;k=e[k].next)
 {
  j=e[k].to;
  if(!dfn[j])//j还没有被访问过
  {
   tarjan(j);
   //父节点是子节点的子节点
   if(low[j]<low[i])
    low[i] = low[j];
  }
  //与j相连,但是j已经被访问过,且还在栈中
  //用子树节点更新节点第一次出现的时间
  else if(instack[j] && dfn[j]<low[i])
   low[i] = dfn[j];
 }
 //节点i是强连通分量的根
 if(dfn[i] == low[i])
 {
 
  //退栈,直至找到根为止
  do
  {
   j = stack[Stop--];
   instack[j] = false;
   Belong[j] = cnt;
  }while(j!=i);
   cnt++;
 }
}
void solve()
{
 Stop = cnt = visitNum = 0;
 memset(dfn,0,sizeof(dfn));

 int i,j;
 for(i=1;i<=N;i++)
  if(!dfn[i])//有可能不是连通图
   tarjan(i);
 for(i=0;i<cnt;i++)
  isMostPopular[i] = true;
 for(i=1;i<=N;i++)
 {
  for(j=head[i];j!=-1;j=e[j].next)
  {
   if(Belong[i]!=Belong[e[j].to])
   {
    isMostPopular[Belong[i]] = false;
    break;
   }
  }
 }
 int num = 0;
 int tpop = 0;
 for(i=0;i<cnt;i++)
 {
  if(isMostPopular[i] == true)
  {
   num++;
   tpop = i;
  }
 }
 int ans = 0;
 if(num==1)
 {
  for(i=1;i<=N;i++)
   if(Belong[i] == tpop)
   {
    ans++;
   }
  cout<<ans<<endl;
 }
 else
  cout<<0<<endl;
}

int main()
{
 int M;
 while(scanf("%d %d",&N,&M)!=EOF)
 {
  int i,j;
  e_num = 0;
  memset(head,-1,sizeof(head));
  for(i=0;i<M;i++)
  {
   int ta,tb;
   scanf("%d%d",&ta,&tb);
   add(ta,tb);
  }
  solve();
 // cout<<cnt<<endl;
 }
 return 0;
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值