POJ 2186 Popular Cows(图论之强连通分量)

本文探讨了如何使用强连通分量算法解决POJ2186牛群人气问题,通过构建有向图并进行拓扑排序,确定哪些牛被认为是最受欢迎的。介绍了两种验证强连通分量的方法,并提供了详细的代码实现。

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

强连通分量之于有向图,与并查集之于无向图,在概念上极其相似,都是寻找互相联系的小部分内容。

POJ 2186 Popular Cows

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. 
--------------------- 
 

思路:

第一道强连通分量题,照着白书写的。

最后判断拓扑排序最后一个强连通分量是否可行看到两种方法,白书是对最后一个连通分量再逆向dfs一遍,验证是否所有点都能到达;看到网上大多的解法是计算每个连通分量的出度,若当且仅当出度为0的只有1个才符合。

 

代码如下:

main.cpp

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 1e4+5;
vector<int> g[maxn];
vector<int> rg[maxn];
vector<int> vs;  // post order
bool used[maxn];
int top[maxn], du[maxn], n, m, k;

void dfs(int u)
{
    used[u] = true;
    for (int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if (!used[v])
        {
            dfs(v);
        }
    }
    vs.push_back(u);
}

void rdfs(int u, int k)
{
    used[u] = 1;
    top[u] = k;
    for (int i = 0; i < rg[u].size(); i++)
    {
        int v = rg[u][i];
        if (!used[v])
        {
            rdfs(v, k);
        }
    }

}

int scc()
{
    memset(used, 0, sizeof(used));
    vs.clear();
    for (int i = 1; i <= n; i++)
    {
        if (!used[i])
        {
            dfs(i);
        }
    }
    memset(used, 0, sizeof(used));
    k = 0;
    for (int i = vs.size()-1; i >= 0; i--)
    {
        if (!used[vs[i]])
        {
            rdfs(vs[i], k);
            k++;
        }
    }
    return k;
}

int main()
{
    while (cin >> n >> m)
    {
        for (int i = 1; i < maxn; i++)
        {
            g[i].clear();
            rg[i].clear();
        }
        for (int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
            rg[v].push_back(u);
        }
        int num = scc();
        int ans = 0, u;
        for (int i = 1; i <= n; i++)
        {
            if (top[i] == num-1)
            {
                u = i;
                ans++;
            }
        }

        // check out whether the last strong componets part of topological sorting
        // can be reached by every other vertices
        memset(used, 0, sizeof(used));
        rdfs(u, 0);
        for (int i = 1; i <= n; i++)
        {
            if (!used[i])
            {
                ans = 0;
                break;
            }
        }
        printf("%d\n", ans);
        continue;

        // check out whether the vertices has one out degree are only one in the strong componets
        memset(du, 0, sizeof(du));
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < g[i].size(); j++)
            {
                if (top[i] != top[g[i][j]])
                {
                    du[top[i]]++;
                }
            }
        }
        int sum = 0;
        for (int i = 0; i < num; i++)
        {
            if (du[i] == 0)
            {
                sum++;
            }
        }
        if (sum == 1)
        {
            printf("%d\n", ans);
        }
        else
        {
            printf("0\n");
        }
    }


    return 0;
}

测试结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值