poj 2186 Popular Cows(Tarjan)

本文介绍了一种算法,用于解决牛群中寻找最受欢迎牛的问题。通过构建图模型并运用Tarjan算法进行强连通分量分析,最终确定能够被所有牛认为是流行的牛的数量。

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

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24548 Accepted: 10072

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


给定牛之间的仰慕关系 且仰慕是可传递的  问 能受到所有牛仰慕的牛的数目。。

如果要受到所有牛的仰慕 则这个牛的点对于其他牛来说 都是可达的

首先对图进行缩点 得到DAG 如果在该DAG中只有一个叶子节点 

则这个节点 都可以由其他节点到达 输出这个点所代表的联通分量中点的个数

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

void read(int &x)
{
    char ch;
    x=0;
    while(ch=getchar(),ch!=' '&&ch!='\n')
    {
        x=x*10+ch-'0';
    }
}

const int MAXN=10010;
const int MAXM=50010;
struct Edge
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN];
int index,top;
int scc;
bool instack[MAXN];
int num[MAXN];
int n,m;

void addedge(int u,int v)
{
    edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;
}

void Tarjan(int u)
{
//    bug;
    int v;
    low[u]=dfn[u]=++index;
    sta[top++]=u;
    instack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
//        bug;
        v=edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
//    bug;
    if(low[u]==dfn[u])
    {
        scc++;
        do{
            v=sta[--top];
            instack[v]=0;
            belong[v]=scc;
            num[scc]++;
        }while(u!=v);
    }
}

int cd[MAXN];
void solve()
{
//    bug;
    MEM(dfn,0);
    MEM(instack,0);
    MEM(cd,0);
    MEM(num,0);
    index=scc=top=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    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])
                cd[belong[u]]++;
        }
    }
    int cnt=0;
    int x;
    for(int i=1;i<=scc;i++)
    {
        if(cd[i]==0)
        {
            cnt++;
            x=i;
        }
    }
    if(cnt==1)
        printf("%d\n",num[x]);
    else puts("0");
}
void init()
{
    tot=0;
    MEM(head,-1);
}

int main()
{
//    fread;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int u,v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        solve();
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值