(POJ 2186)Popular Cows 强连通分量 + 缩点图

Popular Cows

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

题意:
有n只牛,m个关系。每个关系a b 表示a牛认为b牛比较受欢迎,如果a牛认为b牛比较受欢迎,b牛认为c牛比较受欢迎,那么a牛认为c牛比较受欢迎.问你有多少只牛所有的其他牛都认为它比较受欢迎

分析:
题目就是一道求强连通分量的题,首先我们用tarjan算法模板求出所有的强连通分量的信息。
模板详解:http://blog.csdn.net/stillxjy/article/details/70231875
然后所有的强连通分量构成了一个强连通分量图。那么我们将每个强连通分量看成一个大的节点(即缩点)
问题就转换成了在这样一个图中那个节点都可以被其他节点到达。

实现思路:
1:在scc图中,我们要找的点一定是叶子节点,所以记录每个scc节点的出度(outd)即可,叶子节点出度为0
2:若有两个即以上的叶子节点,那么输出0
3:如图不连通,这输出0.(该情况已经被2所包含,因为不连通一定会有两个及以上的叶子节点)

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;

const int maxn = 10010;
struct Edge
{
    int v,next;
}edges[5*maxn];

int n,m,e;          //节点数,边数
int head[maxn];
int pre[maxn];      //节点的时间戳
int sccno[maxn];    //节点的强连通分量的编号
int scc_cnt;        //强连通分量的个数
int dfs_clock;      //时间戳
stack<int> s;

void addedges(int u,int v)  //添加边
{
    edges[e].v = v;
    edges[e].next = head[u];
    head[u] = e++;
}

int dfs(int u)
{
    int lowu = pre[u] = ++dfs_clock;
    s.push(u);
    for(int i=head[u];i!=-1;i=edges[i].next)
    {
        int v = edges[i].v;
        if(!pre[v])                                 //用为访问的子节点更新
        {
            int lowv = dfs(v);
            lowu = min(lowu,lowv);
        }
        else if(!sccno[v]) lowu = min(lowu,pre[v]); //用访问过的但是没有确定编号的节点更新
    }
    if(lowu == pre[u])                              //找到某个强连通分量的第一个节点
    {
        scc_cnt++;
        for(;;)
        {
            int x = s.top(); s.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
    return lowu;
}

int outd[maxn];     //scc节点的出度

int main()
{
    int u,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        e = 0; dfs_clock = 0; scc_cnt = 0;
        memset(head,-1,sizeof(head));
        memset(pre,0,sizeof(pre));
        memset(sccno,0,sizeof(sccno));
        while(s.size()) s.pop();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            addedges(u,v);
        }
        for(int i=1;i<=n;i++)
        {
            if(!pre[i])
            {
                dfs(i);
            }
        }
        memset(outd,0,sizeof(outd));
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=-1;j=edges[j].next)
            {
                if(sccno[i] != sccno[edges[j].v])
                {
                    outd[sccno[i]]++;
                }
            }
        }
        int ansid = 0, ansnum = 0;
        for(int i=1;i<=scc_cnt;i++) if(!outd[i])
        {
            ansid = i;
            ansnum++;
        }
        if(ansnum != 1)
        {
            printf("0\n");
            continue;
        }
        int ans = 0;
        for(int i=1;i<=n;i++)
            if(sccno[i] == ansid) ans++;
        printf("%d\n",ans);

    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值