POJ 2186 有向图的强连通分量

                                                                                            Popular Cows











Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 20542Accepted: 8357

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.
解题思路:首先tarjin缩点,记录每个连通分量的出度,假设出度为零的大于1,则答案为0,否则找出这个连通分量内点的个数。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
struct Edge
{
         int next,to;
}edge[1000000];
int head[10010],stack[10010],belong[10010],out[10010],indexx,tot,top,scc,dfn[10010],low[10010],n,m;
bool instack[10010];
void add(int u,int v)
{
         edge[tot].to=v;
         edge[tot].next=head[u];
         head[u]=tot++;
}
void tarjin(int u)
{
         int i,v;
         low[u]=dfn[u]=++indexx;
         instack[u]=1;stack[top++]=u;
         for(i=head[u];i!=-1;i=edge[i].next)
         {
                  v=edge[i].to;
                  if(!dfn[v])
                  {
                           tarjin(v);
                           if(low[u]>low[v])low[u]=low[v];
                  }
                  else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
         }
         if(low[u]==dfn[u])
         {
                  scc++;
                  do
                  {
                           v=stack[--top];
                           instack[v]=0;
                           belong[v]=scc;
                  }while(v!=u);
         }
}
void solve()
{
         memset(instack,0,sizeof(instack));
         memset(dfn,0,sizeof(dfn));
         memset(out,0,sizeof(out));
         indexx=scc=top=0;
         for(int i=1;i<=n;i++)if(!dfn[i])tarjin(i);
         //cout<<scc<<endl;
         for(int u=1;u<=n;u++)
         {
                  for(int i=head[u];i!=-1;i=edge[i].next)
                  {
                           int v=edge[i].to;
                           if(belong[u]!=belong[v])out[belong[u]]++;break;
                  }
         }
         int bb=0,cc=-1;
         for(int i=1;i<=scc;i++)if(out[i]==0)bb++,cc=i;
         if(bb>1){puts("0");return;}
         int ans=0;
         for(int i=1;i<=n;i++)if(belong[i]==cc)ans++;
         cout<<ans<<endl;
}
void init()
{
      memset(head,-1,sizeof(head));
      tot=0;
      while(m--)
      {
               int a,b;
               scanf("%d%d",&a,&b);
               add(a,b);
      }
      //cout<<tot<<endl;
}
int main()
{
         int i,j,k;
         while(~scanf("%d%d",&n,&m))
         {
                  init();
                  solve();
         }
         return 0;
}

posted @ 2013-09-01 08:45  线性无关  阅读(129)  评论(0)    收藏  举报