POJ 2186 Popular Cows(强连通)

本文介绍了一种通过图论中的强连通分量和有向无环图(DAG)来解决牛群中流行度计算的问题。利用DFS深度优先搜索算法进行强连通分量的划分,并通过统计每个强连通分量的出度来确定哪些牛被认为是最流行的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24039 Accepted: 9865

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. 

题解:若a认为b受欢迎,则建一条边a->b。将图中的强连通分量缩点以后,图一定是个DAG,所以若此时出度为0的点只有一个,那么该点所表示的强连通分量的点则为答案。

代码如下:

#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<math.h>
#include<vector>
#include<string.h>
#define nn 11000
#define inff 0x3fffffff
using namespace std;
int n,m;
struct node
{
    int en,next;
}E[nn*nn];
int p[nn],num;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;
}
int dfn[nn],low[nn],df;
int cnt;
int fa[nn];
bool insta[nn];
stack<int>sta;
void dfs(int id)
{
    dfn[id]=low[id]=++df;
    sta.push(id);
    insta[id]=true;
    int i,w;
    for(i=p[id];i+1;i=E[i].next)
    {
        w=E[i].en;
        if(dfn[w]==-1)
        {
            dfs(w);
            low[id]=min(low[id],low[w]);
        }
        else if(insta[w])
            low[id]=min(low[id],dfn[w]);
    }
    if(low[id]==dfn[id])
    {
        ++cnt;
        int ix;
        while(1)
        {
            ix=sta.top();
            sta.pop();
            insta[ix]=false;
            fa[ix]=cnt;
            if(ix==id)
                break;
        }
    }
}
int cd[nn];
vector<int>ans;
void solve()
{
    memset(cd,0,sizeof(cd));
    memset(insta,false,sizeof(insta));
    memset(dfn,-1,sizeof(dfn));
    df=cnt=0;
    int i,j,w;
    for(i=1;i<=n;i++)
    {
        if(dfn[i]==-1)
        {
            dfs(i);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=p[i];j+1;j=E[j].next)
        {
            w=E[j].en;
            if(fa[i]!=fa[w])
            {
                cd[fa[i]]++;
            }
        }
    }
    int ix=0;
    for(i=1;i<=cnt;i++)
    {
        if(cd[i]==0)
        {
            ix++;
            for(j=1;j<=n;j++)
            {
                if(fa[j]==i)
                {
                    ans.push_back(j);
                }
            }
        }
    }
    if(ix==1)
        printf("%d\n",ans.size());
    else
        puts("0");
}
int main()
{
    int i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v;
        init();
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        ans.clear();
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值