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

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81907387