图论:POJ2186-Popular Cows (求强连通分量)

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.


说实话这个强连通分量看的挺迷的,似懂非懂的(思想看懂了,代码的实现方法不太懂),暂时就不写解题心得了,等以后熟悉了再写。


#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 100;
int dfn[maxn],low[maxn],head[maxn],degree[maxn],_stack[maxn],top,ans,num[maxn],Time;
bool vis[maxn];
struct node
{
    int to,Next;
}edge[maxn];

void add_edge(int m)
{
    int u,v;
    int cnt = 0;
    while(m--)
    {
        scanf("%d%d",&u,&v);
        //注意存图的方法
        edge[cnt].Next = head[u];
        edge[cnt].to = v;
        head[u] = cnt++;
    }
}

void init()
{
    //很烦的初始化但是要看清楚,edge和head是初始化为-1,这样可以便于分别
    Time = 0;
    ans = 0;
    top = 0;
    memset(dfn,0,sizeof(dfn));
    memset(head,-1,sizeof(head));
    memset(low,0,sizeof(low));
    memset(edge,-1,sizeof(edge));
    memset(vis,false,sizeof(vis));
    memset(degree,0,sizeof(degree));
}

void tarjan(int u)
{
    _stack[top] = u;
    top++;
    low[u] = dfn[u] = Time;
    Time++;
    vis[u] = true;
    for(int i=head[u];i!=-1;i=edge[i].Next)
    {
        int v = edge[i].to;
        if(!vis[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);//找到这个点第一次在stack中出现的位置
        }
        else
            low[u] = min(low[u],dfn[v]);
    }

    if(low[u] == dfn[u])//找到一个分量
    {
        ans++;//分量数目加一,从stack中弹出
        while(top>0 && _stack[top] != u)
        {
            top--;
            vis[_stack[top]] = true;
            num[_stack[top]] = ans;
        }
    }
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        add_edge(m);

        for(int i=1;i<=n;i++)
            if(!vis[i])
                tarjan(i);

        int sum = 0,x;
        for(int i=1;i<=n;i++)
            for(int j=head[i];j!=-1;j=edge[j].Next)
                if(num[i] != num[edge[j].to])//计算缩点后每个点的出度
                    degree[num[i]]++;

        for(int i=1;i<=ans;i++)
            if(!degree[i])
            {
                sum++;
                x = i;
            }

        int Ans = 0;
        if(sum == 1)//只能形成一个缩点
        {
            for(int i=1;i<=n;i++)
                if(num[i] == x)
                    Ans++;
            printf("%d\n",Ans);
        }
        else
            printf("0\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值