POJ 2186 Popular Cows 强连通

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25060 Accepted: 10268

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


周日时准备讲的

题意:求有向图中能被所有点到达的点数

缩点后,出度为0且唯一的连通块包含的点数为解-w-


/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
//#include <tuple>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
    return a==0?b:gcd(b%a,a);
}

#define INF 1e9
#define eps 1e-8
#define mod 10007
#define maxn 10010
#define maxm 50010

using namespace std;

int v[maxn],col[maxn],ans[maxn];
int rea[maxn],low[maxn],stack[maxn];
int out[maxn];
int n,m,tot=0,color,tm=0,index=0,top=0;
int pre[maxn];

struct Side
{
    int to,next;
}e[maxm];

void add(int u,int v)
{
    e[tot].to=v;
    e[tot].next=pre[u];
    pre[u]=tot++;
}

void tarjan(int i)
{
    v[i]=1;
    top++;
    stack[top]=i;
    ++tm;
    rea[i]=tm;
    low[i]=tm;
    for(int j=pre[i];j!=-1;j=e[j].next)
        {
            int x=e[j].to;
            if(v[x]==0) tarjan(x);
            if(v[x]<2) low[i]=min(low[i],low[x]);
        }
    if(rea[i]==low[i])
    {
        color++;
        int tt=0;
        while(stack[top+1]!=i)
        {
            tt++;
            col[stack[top]]=color;
            v[stack[top]]=2;
            top--;
        }
        ans[color]=tt;
    }

}

void init()
{
    mem(col);
    mem(v);
    mem1(pre);
    mem(rea);
    mem(low);
    tot=0;
    top=0;
    tm=0;
    index=0;
    color=0;
    mem(out);
}

int main()
{
     //freopen("in.txt","r",stdin);
     int uu,vv;
     while(~sc2(n,m))
     {
        init();
        lp(i,m)
          {
              sc2(uu,vv);
              add(uu,vv);
          }

          lp(i,n)
           if(!rea[i])
              tarjan(i);
          if(color==1)
         {
            printf("%d\n",n);
            continue;
         }
         lp(i,n)
         {
             for(int j=pre[i];j!=-1;j=e[j].next)
            {
               int x=e[j].to;
               if(col[i]!=col[x])
                 out[col[i]]++;
            }
         }

         int re=0;
         lp(i,color)
         {
             if(!out[i])
            {
                if(!re)
                    re=ans[i];
                else
                {
                    re=0;
                    break;
                }
            }

         }
         printf("%d\n",re);

     }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值