poj2186Popular Cows(强连通+缩点)

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19647 Accepted: 7960

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,然后缩点,求新图中出度为0的点的个数,如果出度为0的点只有1个则这个强连通所有点都能被其他点可达,否则没有点能被所有点可达。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 10005;
const int M = 50005;
struct edge
{
    int to,next;
}g[M];
int head[N];
int scc[N];
int cnt[N];//统计强连通的点数
int stack1[N];
int stack2[N];
int vis[N];
int out[N];
int m,n;

void init()
{
    int i;
    for(i = 1;i <= n;i ++)
    {
        head[i] = -1;
        scc[i] = cnt[i] = vis[i] = out[i] = 0;
    }
}
int nextint()
{
    char c;
    int ret;
    while((c = getchar()) > '9' || c < '0')
        ;
    ret = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        ret = ret * 10 + c - '0';
    return ret;
}

void dfs(int cur,int &sig,int &ret)
{
    vis[cur] = ++sig;
    stack1[++stack1[0]] = cur;
    stack2[++stack2[0]] = cur;
    for(int i = head[cur];i != -1;i = g[i].next)
    {
        if(vis[g[i].to] == 0)
            dfs(g[i].to,sig,ret);
        else
            if(scc[g[i].to] == 0)
            {
                while(vis[stack2[stack2[0]]] > vis[g[i].to])
                    stack2[0] --;
            }
    }
    if(stack2[stack2[0]] == cur)
    {
        ++ret;
        stack2[0] --;
        do
        {
            scc[stack1[stack1[0]]] = ret;
            cnt[ret] ++;
        }while(stack1[stack1[0] --] != cur);
    }
}

int Gabow()
{
    int i,sig,ret;
    stack1[0] = stack2[0] = sig = ret = 0;
    for(i = 1;i <= n;i ++)
        if(!vis[i])
            dfs(i,sig,ret);
    return ret;
}

int solve()
{
    int ret,i,j;
    int num = Gabow();
    if(num == 1)
        return n;
    for(i = 1;i <= n;i ++)
    {
        for(j = head[i];j != -1;j = g[j].next)
        {
            if(scc[i] != scc[g[j].to])
                out[scc[i]] ++;
        }
    }
    int ct = 0;
    int ci;
    for(i = 1;i <= num;i ++)
    {
        if(out[i] == 0)
        {
            ct ++;
            ci = i;
        }
    }
    if(ct == 1)
        return cnt[ci];
    else
        return 0;
}

int main()
{
    int i,j,a,b;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(i = 1;i <= m;i ++)
        {
            a = nextint();
            b = nextint();
            g[i].to = b;
            g[i].next = head[a];
            head[a] = i;
        }
        printf("%d\n",solve());
    }
    return 0;
}
//1468K	16MS  G++
//824K	110MS C++




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值