zoukankan      html  css  js  c++  java
  • poj 2186 Popular Cows tarjan

    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. 
    low数组的理解:对于一个点,能从未遍历的边到达的最低的深度优先级
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll __int64
    #define inf 2000000001
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF )  return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    struct is
    {
        int u,v;
        int next;
    }edge[50010];
    int head[50010];
    int belong[50010];
    int dfn[50010];
    int low[50010];
    int stackk[50010];
    int instack[50010];
    int number[50010];
    int du[100010];
    int n,m,jiedge,lu,bel,top;
    void update(int u,int v)
    {
        jiedge++;
        edge[jiedge].u=u;
        edge[jiedge].v=v;
        edge[jiedge].next=head[u];
        head[u]=jiedge;
    }
    void dfs(int x)
    {
        dfn[x]=low[x]=++lu;
        stackk[++top]=x;
        instack[x]=1;
        for(int i=head[x];i;i=edge[i].next)
        {
            if(!dfn[edge[i].v])
            {
                dfs(edge[i].v);
                low[x]=min(low[x],low[edge[i].v]);
            }
            else if(instack[edge[i].v])
            low[x]=min(low[x],dfn[edge[i].v]);
        }
        if(low[x]==dfn[x])
        {
            int sum=0;
            bel++;
            int ne;
            do
            {
                sum++;
                ne=stackk[top--];
                belong[ne]=bel;
                instack[ne]=0;
            }while(x!=ne);
            number[bel]=sum;
        }
    }
    void tarjan()
    {
        memset(dfn,0,sizeof(dfn));
        bel=lu=top=0;
        for(int i=1;i<=n;i++)
        if(!dfn[i])
        dfs(i);
    }
    int main()
    {
        int i,t;
        while(~scanf("%d%d",&n,&m))
        {
            memset(head,0,sizeof(head));
            jiedge=0;
            for(i=1;i<=m;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                update(u,v);
            }
            tarjan();
            for(i=1;i<=jiedge;i++)
            if(belong[edge[i].v]!=belong[edge[i].u])
            du[belong[edge[i].u]]++;
            int flag=0,pos;
            /*for(i=1;i<=n;i++)
            cout<<belong[i]<<endl;*/
            for(i=1;i<=bel;i++)
            {
                if(!du[i])
                {
                    flag++;
                    pos=i;
                }
            }
            if(flag!=1)
            printf("0
    ");
            else
            printf("%d
    ",number[pos]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    信息学奥赛一本通(c++版) 1003:对齐输出
    读书笔记(华科曹计昌 《c语言与程序设计》)
    使用request对象实现注册示例,get/post的编码问题
    Eclipse中开发第一个web(jsp)项目
    Eclipse恢复默认布局
    手工在tomcat目录中建立个人项目
    通过ServletContext获得工程根目录路径、读取文件以及获得classpath目录下的文件
    ServletContext设置全局变量实现统计站点访问次数
    servlet全局参数的设置
    Eclipse关联Servlet源码详细步骤
  • 原文地址:https://www.cnblogs.com/jhz033/p/5360779.html
Copyright © 2011-2022 走看看