• POJ_2186_Tarjan Popular_Cows


    popular_cow
    #include <cstdio>
    #include <cstring>
    #include <climits> // INT_MAX
    #include <vector>
    #include <stack>
    #include <algorithm>
    
    #define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
    
    const int N = 10000;
    
    int n, m;
    std::vector <int> graph[N], new_graph[N];
    
    int depth[N], lowest[N], scc_count, id[N];
    std::stack <int> stack;
    
    void dfs(int p, int u) {
        if (depth[u] == -1) {
            int tmp = lowest[u] = depth[u] = p == - 1 ? 0 : depth[p] + 1;
            stack.push(u);
            foreach (iter, graph[u]) {
                int v = *iter;
                dfs(u, v);
                tmp = std::min(tmp, lowest[v]);
            }
            lowest[u] = tmp;
            if (depth[u] == lowest[u]) {
                while (true) {
                    int v = stack.top();
                    stack.pop();
                    id[v] = scc_count;
                    lowest[v] = INT_MAX; 
                    if (u == v) {
                        break;
                    }
                }
                scc_count ++;
            }
        }
    }
    
    bool vis[N];
    
    int main() {
        scanf("%d%d", &n, &m);
        for (int i = 0, a, b; i < m; ++ i) {
            scanf("%d%d", &a, &b);
            a --;
            b --;
            graph[b].push_back(a);
        }
        memset(depth, -1, sizeof(depth));
        scc_count = 0;
        for (int i = 0; i < n; ++ i) {
            dfs(-1, i);
        }
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; ++ i) {
            foreach (iter, graph[i]) {
                int j = *iter;
                if (id[i] != id[j]) {
                    vis[id[j]] = true;
                }
            }
        }
        int ans = 0, cnt = 0, scc_ko;
        for (int i = scc_count - 1; i >= 0; -- i) {
            if (!vis[i]) {
                cnt++;
                scc_ko = i;
            }
        }
        if (cnt != 1) printf("0
    ");
        else {
            for (int i = 0; i < n; i++) {
                if (id[i] == scc_ko) ++ans;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

  • 相关阅读:
    Log4net<转载>
    XSD使用《转载》
    assemble文件中配置
    常用工具《收藏》
    mysql查看所有存储过程,函数,视图,触发器,表《转》
    log4g net 配置
    XSLT使用<转载>
    C#操作xml之xpath语法<收藏>
    如何做镜像服务器
    Android开发之旅:环境搭建及HelloWorld
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786775.html
Copyright © 2020-2023  润新知