• 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
    【题意】:给你一个有向图,问你有多少个点可以被其它所有点访问到。

    我们可以用tarjia算法去掉所有的环,然后重构图后就变成了,找出出度为零的点就行了

    https://www.byvoid.com/zhs/blog/scc-tarjan

    https://blog.csdn.net/qq_34374664/article/details/77488976

    /********************************
    tarjan + 图的重构  
    *********************************/
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <stack>
    
    using namespace std;
    
    const int maxn = 1e4+50;
    
    stack<int>sta;
    vector<int>G[maxn];
    int dfn[maxn],low[maxn],tag[maxn];
    int index,scc,id[maxn];
    bool vis[maxn];
    int n,m;
    
    void init() {
        index = 0,scc = 0;
        memset(vis,false,sizeof(vis));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(tag,0,sizeof(tag));
        memset(id,0,sizeof(id));
        while(!sta.empty()) sta.pop();
        for (int i = 1;i<=n;i++) G[i].clear();
    }
    
    void tarjan(int x) {  //强联通tarjan 
        dfn[x] = low[x] = ++index;
        vis[x] = true; sta.push(x);
        int len = G[x].size();
        for (int i = 0 ; i < len;i++ ) {
            int to = G[x][i];
            if ( !vis[to] ) {
                tarjan(to);  low[x] = min(low[x],low[to]);
            } else if ( vis[to] ) {
                low[x] = min(low[x],dfn[to]);
            }
        }
        if(low[x] == dfn[x]) {  // ac代码,跟re代码区别不大关键是取栈顶的位置 
            scc++;
            int v;
            do{
                v = sta.top();
                tag[v] = scc;
                id[scc]++;
                sta.pop();
            }while(x != v);
        }
    //  if(low[x] == dfn[x]) {   //re代码,栈越界 
    //      scc++;
    //      int v = sta.top();
    //      do{
    //          tag[v] = scc;
    //          id[scc]++;
    //          sta.pop();
    //          v = sta.top();
    //      }while(x!=v);
    //  }
    }
    
    int main(){
        while(~scanf("%d %d",&n,&m)){
            int u,v;
            init();
            for (int i = 1 ; i <= m ; i ++) {
                scanf("%d %d",&u,&v);
                G[u].push_back(v);
            }
            for (int i = 1;i <= n; i ++ ) {
                if(!dfn[i]) tarjan(i);
            } 
            memset(vis,false,sizeof(vis));
            for (int i = 1;i <= n;i++ ) {  //利用tarjan算法缩点重构图 
                int len = G[i].size();
                for (int j = 0;j < len;j++) {
                        if(tag[i] != tag[G[i][j]] ) {
                        vis[tag[i]] = true;
                    }
                }
            }
            int pos,sum = 0;
            for (int i = 1;i<=scc;i++) {
                if(!vis[i]) sum++,pos = i;
            }
            if(sum == 1) {
                printf("%d
    ",id[pos]);
            } else printf("0
    ");
        }
        return 0;
    }
    /*
    3 3
    1 2
    2 1
    2 3
    */
  • 相关阅读:
    Douglas-Peucker 轨迹压缩算法
    Marching squares 算法 获取轮廓(contour tracing)
    创建Mesh->格子地图转NavMesh->可破坏墙壁
    StretchedBillboard 实现
    程序员的微创业
    买云服务器有推荐吗?国内知道有腾讯云、阿里云...等等,不知道该选哪个好了,另外优惠吗?
    我的阿里云5年
    2021阿里云、腾讯云、华为云、滴滴云评测比较
    终于找到可以一文多发的平台了!
    2019年最新阿里云主机优惠购买指南
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745960.html
Copyright © 2020-2023  润新知