您的位置:首页 > 运维架构

poj2186 Popular Cows(tarjan + 缩点)

2014-08-12 17:13 232 查看
Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23312 Accepted: 9541
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

USACO 2003 Fall

先求出各个强连通分量,缩点后,求出 出度为0的点的这个分量的  点的个数,

特别注意 当有多个出度为0的点事,输出0

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <climits>

#include <vector>

#include <queue>

#include <cstdlib>

#include <string>

#include <set>

#include<stack>

using namespace std;

const int N=10010;

int dfn
,lowlink
,instack
;

stack<int> s;

int sign,num
,ans;

int in
,out
;

vector<int> g
;

int n,m;

void init()

{

    for(int i=0;i<N;i++)

        g[i].clear();

    memset(dfn,0,sizeof(dfn));

    memset(lowlink,0,sizeof(lowlink));

    memset(num,0,sizeof(num));

}

void add_edge(int from,int to)

{

    g[from].push_back(to);

}

void tarjan(int u)

{

    dfn[u]=lowlink[u]=sign;sign++;

    instack[u]=1;

    s.push(u);

    for(int i=0;i<g[u].size();i++)

    {

        int x=g[u][i];

        if(!dfn[x])

        {

            tarjan(x);

            lowlink[u]=min(lowlink[u],lowlink[x]);

        }

        else if(instack[x])

            lowlink[u]=min(lowlink[u],dfn[x]);

    }

    if(dfn[u]==lowlink[u])

    {

        ans++;

        int t=-1;

        while(t!=u)

        {

            t=s.top();

            s.pop();

            instack[t]=0;

            num[t]=ans;

        }

    }

}

int solve()

{

    //cout<<"ans="<<ans<<endl;

   // cout<<num[1]<<num[2]<<num[3]<<endl;

    memset(in,0,sizeof(in));

    memset(out,0,sizeof(out));

    for(int i=1;i<=n;i++)

    {

        for(int j=0;j<g[i].size();j++)

        {

            int x=g[i][j];

            if(num[i]!=num[x])

            {

                //cout<<i<<" "<<j<<endl;

                out[num[i]]++;

                in[num[x]]++;

            }

        }

    }

    //cout<<in[1]<<in[2]<<endl;

    //cout<<out[1]<<out[2]<<endl;

    int cnt=0;

    int flag;

    for(int i=1;i<=ans;i++)

    {

        if(out[i]==0)

        {

            flag=i;

            cnt++;

        }

    }

    int ans=0;

    //cout<<num[1]<<num[2]<<num[3]<<endl;

    //cout<<flag<<endl;

    if(cnt>1)

        return 0;

    for(int i=1;i<=n;i++)

    {

        if(num[i]==flag)

            ans++;

    }

    return ans;

}

int main()

{

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        init();

        for(int i=0;i<m;i++)

        {

            int aa,bb;

            scanf("%d%d",&aa,&bb);

            add_edge(aa,bb);

        }

        ans=0;

        sign=1;

        for(int i=1;i<=n;i++)

        {

            if(!dfn[i])

                tarjan(i);

        }

        printf("%d\n",solve());

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: