zoukankan      html  css  js  c++  java
  • poj2186 强连通缩点

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 29141   Accepted: 11779

    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

    题意:要求找到所有点都能遍历到的点的个数
    思路:强连通分量缩点(环缩点),化成DAG图,也就是树或者深林,讨论有没有出度为0的点,记录这些点的sum,如果sum=1,说明该点所有其他点都能遍历得到,sum>1,说明此图不存在这样的点,【(1->2,1-> 3)这种情况或者图为森林】
    找到该点后,记录该点缩点前的个数
    强连通缩点的算法依然用targan,栈处理有点和前面的判断缩点,双连通分量不同,判断条件为low[u]==dfn[u]
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    
    const int inf = 0x3f3f3f;
    const int MAXN = 1e4+10;
    const int MMAXN = 5e4+10;
    struct edge{
      int st;
      int to;
      int next;
      int vis;
    };
    
    using namespace std;
    edge e[MMAXN];
    stack<int>s;
    int first[MAXN];
    int dfn[MAXN];
    int low[MAXN];
    int mark[MAXN];
    int out[MAXN];
    int top,sum,cur,ans;
    int flag;
    int n,m;
    
    void init(){
        memset(first,-1,sizeof(first));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(mark,-1,sizeof(mark));
        memset(out,0,sizeof(out));
        top = sum = cur = flag = ans = 0;
    }
    
    void addedge(int u,int v){
        e[top].st = u;
        e[top].to = v;
        e[top].vis = 0;
        e[top].next = first[u];
        first[u] = top++;
    }
    
    void dfs(int u,int t){
        dfn[u] = low[u] = t;
        int v;
        s.push(u);
        for(int i=first[u];i!=-1;i=e[i].next){
            v = e[i].to;
            if(!dfn[v]){
                dfs(v,t+1);
                low[u] = min(low[u],low[v]);
            }
            else{
                low[u] = min(low[u],dfn[v]);
            }
        }
    
        if(low[u]==dfn[u]){
            /*cout<<"********"<<endl;
            cout<<u<<endl;
            cout<<"********"<<endl;*/
            while(s.top()!=u){
                mark[s.top()] = cur;
                s.pop();
            }
            mark[s.top()] = cur;
            s.pop();
            cur++;
        }
    }
    
    int main()
    {
       // init();
        int a,b,x;
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            for(int i=0;i<m;i++){
                scanf("%d%d",&a,&b);
                addedge(a,b);
            }
            for(int i=1;i<=n;i++){
                if(!dfn[i])
                    dfs(i,1);
            }
            /*for(int i=1;i<=n;i++){
                cout<<mark[i]<<" ";
            }
            cout<<endl;*/
            for(int i=1;i<=n;i++){
                for(int j=first[i];j!=-1;j=e[j].next){
                    int v = e[j].to;
                    if(mark[i]!=mark[v]){
                        out[mark[i]]++;
                    }
                }
            }
            /*for(int i=0;i<cur;i++){
                cout<<out[i]<<" ";
            }
            cout<<endl;*/
            for(int i=0;i<cur;i++){
                if(out[i]==0){
                    sum++;
                    x = i;
                }
            }
            if(sum!=1){
                cout<<0<<endl;
                continue;
            }
            for(int i=1;i<=n;i++){
                if(mark[i]==x)ans++;
            }
            cout<<ans<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    convex hull
    不努力,你就没有资格谈天赋
    “数数”的感悟
    平行四边形法则的感悟
    ansys 15 Linux 安装问题
    SHV-E210S 刷4.3
    fedora 安装 Oracle11g 遇到的问题
    ObjectArx开发常用资料
    MFC入门(二)
    MFC入门(三)
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5456258.html
Copyright © 2011-2022 走看看