Poj 2186- Popular Cows//kosaraju

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19089 Accepted: 7678

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


分析:刚开始理解错题意了,以为是找出最受欢迎的牛,结果发现不是。就是找到每头牛都觉得该头牛牛x的个数。也就是在一个DAG中找出出度为零的点,如果有两个出度为零的点,则不可能有这样的一头牛。

方法:一、求强连通分支,将图转为DAG

            二、在DAG中找到出度为零的连通分支或点。

代码:

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 10010;
vector<int> G[maxn],Gt[maxn];
int ord[maxn];
int vis[maxn];
int out[maxn]; 
int ss[maxn];
int ans[maxn];
int cnt;
int t;
int n,m;
void dfs_1(int u)
{
    vis[u] = 1;
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!vis[v]) dfs_1(v);
    }
    ord[t++] = u;
}
void dfs_2(int u)
{
    vis[u] = 1;
    ss[u] = cnt;
    for(int i = 0; i < Gt[u].size(); i++)
    {
        int v = Gt[u][i];
        if(!vis[v])
        {
            ans[cnt]++;
            dfs_2(v);
        }
    }
}
void kosaraju()
{
    int flag = -1;
    t = 1;
    cnt = 1;
    for(int i = 1; i <= n; i++) ans[i] = 1;
    memset(out,0,sizeof(out));
    memset(vis,0,sizeof(vis));
    for(int i = 1; i <= n; i++) if(!vis[i]) dfs_1(i);
    memset(vis,0,sizeof(vis));
    for(int i = t - 1; i >= 1; i--)
    {
        int v = ord[i];
        if(!vis[v])
        {
            dfs_2(v);
            cnt++;
        }
    }
    //构造DAG
    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j < G[i].size(); j++)
        {
            if(ss[i] == ss[G[i][j]]) continue;
            out[ss[i]]++;
        }
    }
    //查找出度为零的点或者连通分支
    for(int i = 1; i < cnt; i++)
    {
        if(!out[i] && flag == -1) flag = ans[i];
        else if(!out[i] && flag != -1)
        {
            flag = 0;
            break;
        }
    }
    printf("%d\n",flag);
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        for(int i = 1; i <= m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            Gt[v].push_back(u);
        }
        kosaraju();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值