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\n");
    else {
        for (int i = 0; i < n; i++) {
            if (id[i] == scc_ko) ++ans;
        }
        printf("%d\n", ans);
    }
    return 0;
}

posted @ 2014-05-08 09:39  小尼人00  阅读(92)  评论(0编辑  收藏  举报