zoukankan      html  css  js  c++  java
  • 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;
    }
    

  • 相关阅读:
    php的命名空间层级与目录层级是一致的吗?
    PHP 反射应用之一(插件框架)
    一次http完整的请求tcp报文分析
    Restful based service 的跨域调用
    php 命名空间的目的
    浏览器跨域问题
    PHP 代码跟踪
    记一次分析别人源码的过程
    php script 的生命周期
    全局安装 vue
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786775.html
Copyright © 2011-2022 走看看