zoukankan      html  css  js  c++  java
  • 图论:POJ2186-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.


    说实话这个强连通分量看的挺迷的,似懂非懂的(思想看懂了,代码的实现方法不太懂),暂时就不写解题心得了,等以后熟悉了再写。


    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn = 1e5 + 100;
    int dfn[maxn],low[maxn],head[maxn],degree[maxn],_stack[maxn],top,ans,num[maxn],Time;
    bool vis[maxn];
    struct node
    {
        int to,Next;
    }edge[maxn];
    
    void add_edge(int m)
    {
        int u,v;
        int cnt = 0;
        while(m--)
        {
            scanf("%d%d",&u,&v);
            //注意存图的方法
            edge[cnt].Next = head[u];
            edge[cnt].to = v;
            head[u] = cnt++;
        }
    }
    
    void init()
    {
        //很烦的初始化但是要看清楚,edge和head是初始化为-1,这样可以便于分别
        Time = 0;
        ans = 0;
        top = 0;
        memset(dfn,0,sizeof(dfn));
        memset(head,-1,sizeof(head));
        memset(low,0,sizeof(low));
        memset(edge,-1,sizeof(edge));
        memset(vis,false,sizeof(vis));
        memset(degree,0,sizeof(degree));
    }
    
    void tarjan(int u)
    {
        _stack[top] = u;
        top++;
        low[u] = dfn[u] = Time;
        Time++;
        vis[u] = true;
        for(int i=head[u];i!=-1;i=edge[i].Next)
        {
            int v = edge[i].to;
            if(!vis[v])
            {
                tarjan(v);
                low[u] = min(low[u],low[v]);//找到这个点第一次在stack中出现的位置
            }
            else
                low[u] = min(low[u],dfn[v]);
        }
    
        if(low[u] == dfn[u])//找到一个分量
        {
            ans++;//分量数目加一,从stack中弹出
            while(top>0 && _stack[top] != u)
            {
                top--;
                vis[_stack[top]] = true;
                num[_stack[top]] = ans;
            }
        }
    }
    
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            init();
            add_edge(m);
    
            for(int i=1;i<=n;i++)
                if(!vis[i])
                    tarjan(i);
    
            int sum = 0,x;
            for(int i=1;i<=n;i++)
                for(int j=head[i];j!=-1;j=edge[j].Next)
                    if(num[i] != num[edge[j].to])//计算缩点后每个点的出度
                        degree[num[i]]++;
    
            for(int i=1;i<=ans;i++)
                if(!degree[i])
                {
                    sum++;
                    x = i;
                }
    
            int Ans = 0;
            if(sum == 1)//只能形成一个缩点
            {
                for(int i=1;i<=n;i++)
                    if(num[i] == x)
                        Ans++;
                printf("%d
    ",Ans);
            }
            else
                printf("0
    ");
        }
        return 0;
    }
  • 相关阅读:
    Mysql 执行安装脚本报错Changed limits:
    Centos6.6 安装Mysql集群
    Oracle11g RAC+DG搭建
    Oracle用函数或PIVOT实现行转列
    Oracle根据列中的特殊符号进行分组
    Hadoop on Windows with Eclipse -02- Prerequisites
    Hadoop on Windows with Eclipse -01- Introduction
    Hadoop入门之WordCount运行详解
    Hadoop namenode无法启动问题解决
    jar 打包命令详解
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107266.html
Copyright © 2011-2022 走看看