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

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

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

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;
}

测试结果如下:

**高校专业实习管理平台设计与实现** 本设计项目旨在构建一个服务于高等院校专业实习环节的综合性管理平台。该系统采用当前主流的Web开发架构,基于Python编程语言,结合Django后端框架与Vue.js前端框架进行开发,实现了前后端逻辑的分离。数据存储层选用广泛应用的MySQL关系型数据库,确保了系统的稳定性和数据处理的效率。 平台设计了多角色协同工作的管理模型,具体包括系统管理员、院系负责人、指导教师、实习单位对接人以及参与实习的学生。各角色依据权限访问不同的功能模块,共同构成完整的实习管理流程。核心功能模块涵盖:基础信息管理(如院系、专业、人员信息)、实习过程管理(包括实习公告发布、实习内容规划、实习申请与安排)、双向反馈机制(单位评价与学生反馈)、实习支持与保障、以及贯穿始终的成绩评定与综合成绩管理。 在技术实现层面,后端服务依托Django框架的高效与安全性构建业务逻辑;前端界面则利用Vue.js的组件化特性与LayUI的样式库,致力于提供清晰、友好的用户交互体验。数据库设计充分考虑了实习管理业务的实体关系与数据一致性要求,并保留了未来功能扩展的灵活性。 整个系统遵循规范的软件开发流程,从需求分析、系统设计、编码实现到测试验证,均进行了多轮迭代与优化,力求在功能完备性、系统性能及用户使用体验方面达到较高标准。 **核心术语**:实习管理平台;Django框架;MySQL数据库;Vue.js前端;Python语言。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值