zoukankan      html  css  js  c++  java
  • POJ 2186 Popular Cows (两种方法)

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19083   Accepted: 7675

    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. 

    Source

     
     
    题意:有N头牛 每一头牛都梦想着成为popular cow,(但这是不可能滴) 有m组仰慕的关系,仰慕有传递性 比如说A觉得B是popular and B thinks C is popular, then A thinks C is popalur also;
    现在问有多少头牛是会被其他牛都仰慕。

    思路:求强连通分量,缩成点 点内的头当然是相互仰慕的咯!! 然后求新的图 的出度 出度也0的点就会被所有牛仰慕 算出出度为0的强连通分量里点的个数就OK了,注意 可能有多个出度为0的点,这时输出0(不满足要求) 刚学korasaju 算法,所以用它做 详解见上一篇
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int VM=10010;
    const int EM=50010;
    
    struct Edge{
        int to,nxt;
    }edge1[EM],edge2[EM];
    
    int head1[VM],head2[VM],n,cnt;
    int scc_id[VM],vis[VM],order[VM],k,scc;
    int indeg[VM],outdeg[VM];
    
    void addedge(int cu,int cv){
        edge1[cnt].to=cv;
        edge1[cnt].nxt=head1[cu];
        head1[cu]=cnt;
    
        edge2[cnt].to=cu;
        edge2[cnt].nxt=head2[cv];
        head2[cv]=cnt++;
    }
    
    void DFS(int u){
        for(int i=head1[u];i!=-1;i=edge1[i].nxt){
            int v=edge1[i].to;
            if(!vis[v]){
                vis[v]=1;
                DFS(v);
            }
        }
        order[++k]=u;
    }
    
    void reDFS(int u){
        for(int i=head2[u];i!=-1;i=edge2[i].nxt){
            int v=edge2[i].to;
            if(!scc_id[v]){
                scc_id[v]=scc;
                reDFS(v);
            }
        }
    }
    
    void Korasaju(){
        int i,u;
        k=scc=0;
        memset(vis,0,sizeof(vis));
        memset(scc_id,0,sizeof(scc));
        memset(order,0,sizeof(order));
        for(u=1;u<=n;u++)
            if(!vis[u]){
                vis[u]=1;
                DFS(u);
            }
        for(i=n;i>0;i--){
            u=order[i];
            if(!scc_id[u]){
                scc_id[u]=++scc;
                reDFS(u);
            }
        }
    }
    
    void Count_deg(){
        memset(indeg,0,sizeof(indeg));
        memset(outdeg,0,sizeof(outdeg));
        for(int u=1;u<=n;u++)
            for(int i=head1[u];i!=-1;i=edge1[i].nxt){
                int v=edge1[i].to;
                if(scc_id[u]!=scc_id[v]){
                    indeg[scc_id[v]]++;
                    outdeg[scc_id[u]]++;
                }
            }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int m;
        while(~scanf("%d%d",&n,&m)){
            memset(head1,-1,sizeof(head1));
            memset(head2,-1,sizeof(head2));
            cnt=0;
            int u,v;
            while(m--){
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            Korasaju();
            Count_deg();
            int count=0,loc;
            for(int i=1;i<=scc;i++)
                if(outdeg[i]==0){
                    count++;
                    loc=i;
                }
            if(count>1)
                printf("0\n");
            else{
                count=0;
                for(u=1;u<=n;u++)
                    if(scc_id[u]==loc)
                        count++;
                printf("%d\n",count);
            }
        }
        return 0;
    }
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int VM=10010;
    const int EM=50010;
    
    struct Edge{
        int to,nxt;
    }edge[EM<<1];
    
    int n,m,cnt,dep,top,atype,head[VM];
    int dfn[VM],low[VM],outdeg[VM],vis[VM],belong[VM];
    int stack[VM];
    
    void Init(){
        cnt=0,dep=0,top=0,atype=0;
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(outdeg,0,sizeof(outdeg));
        memset(vis,0,sizeof(vis));
        memset(belong,0,sizeof(belong));
    }
    
    void addedge(int cu,int cv){
        edge[cnt].to=cv;
        edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
    }
    
    void Tarjan(int u){
        dfn[u]=low[u]=++dep;
        stack[top++]=u;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(!dfn[v]){
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }else if(vis[v])
                low[u]=min(low[u],dfn[v]);
        }
        int j;
        if(dfn[u]==low[u]){
            atype++;
            do{
                j=stack[--top];
                belong[j]=atype;
                vis[j]=0;
            }while(u!=j);
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            Init();
            int u,v;
            while(m--){
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            for(int i=1;i<=n;i++)
                if(!dfn[i])
                    Tarjan(i);
            int tmp;
            for(int i=1;i<=n;i++){
                tmp=belong[i];
                for(int j=head[i];j!=-1;j=edge[j].nxt){
                    int v=edge[j].to;
                    if(belong[i]!=belong[v])
                        outdeg[belong[i]]++;
                }
            }
            cnt=0;
            int k;
            for(int i=1;i<=atype;i++)
                if(outdeg[i]==0){
                    cnt++;
                    k=i;
                }
            if(cnt>1)
                printf("0\n");
            else{
                int ans=0;
                for(int i=1;i<=n;i++)
                    if(belong[i]==k)
                        ans++;
                printf("%d\n",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    一次“半面试”——汉得
    检查随机序列重复[Java]
    设计模式 工厂方法(Factory Method Pattern)转载
    sprintf用法详解
    MFC ComboBox的使用
    Visual Studio 2008 中程序路径配置 .
    Visual Studio 2008 、 Windows 环境变量介绍 和 如何在文件中引用环境变量 .
    MFC总结之CListCtrl用法及技巧(二) .
    C/C++中Sqlite使用简介
    getenv、setenv函数 获取和设置系统环境变量
  • 原文地址:https://www.cnblogs.com/jackge/p/3056439.html
Copyright © 2011-2022 走看看