Popular Cows (强连通分量加缩点)

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
    题意:给定一个有向图,求有多少个顶点可有其他任意顶点出发可达。
    需要用到的定理:DAG中唯一出度为0的点可由其他任意点到达,求出原图的强连通分量,将原图缩成DAG。
    AC的C++程序如下:
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn = 50005;
int n, m;
vector<int> g[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    s.push(u);
    for (int i = 0; i < (int)g[u].size(); i++)
    {
        int v = g[u][i];
        if (!pre[v])
        {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        }
        else if (!sccno[v])
        {
            lowlink[u] = min(lowlink[u], pre[v]);
        }
    }
    if (lowlink[u] == pre[u])
    {
        scc_cnt++;
        for (;;)
        {
            int x = s.top(); s.pop();
            sccno[x] = scc_cnt;
            if (x == u) break;
        }
    }
}
int main()
{
        cin >> n >> m;
        dfs_clock = scc_cnt = 0;
        memset(sccno, 0, sizeof(sccno));
        memset(pre, 0, sizeof(pre));
        memset(lowlink, 0, sizeof(lowlink));
        for (int i = 0; i <= n; i++) g[i].clear();
        while (!s.empty()) s.pop();
        int src, dest;
        for (int i = 1; i <= m; i++)
        {
            cin >> src >> dest;
            g[src].push_back(dest);
        }
        for (int i = 1; i <= n; i++)
        {
            if (!pre[i]) dfs(i);
        }
        int out[maxn];
        for (int i = 1; i <= scc_cnt; i++) out[i] = 1;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0;j < (int)g[i].size(); j++)
            {
                int v = g[i][j];
                if (sccno[i] != sccno[v]) out[sccno[i]] = 0;
            }
        }
        int no = 0, sum = 0, sumno = 0;
        for (int i = 1; i <= scc_cnt; i++)
        {
            if (out[i])
            {
                  sumno++, no = i;
            }
        }   
        if (sumno > 1) cout << 0 << endl;
        else {
            for (int i = 1; i <= n; i++)
            {
                if (sccno[i] == no) sum++;
            }
            cout << sum << endl;
        }
       return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值