POJ3180——The Cow Prom

大致思路:

根据题目描述,能转的起来的一定是这个有向图的强连通分量,而且分量里的点数大于1。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#pragma comment(linker, "/STACK:1024000000,1024000000")

#define     IT              iterator
#define     PB(x)           push_back(x)
#define     CLR(a,b)        memset(a,b,sizeof(a))

using namespace std;

typedef     long long               ll;
typedef     unsigned long long      ull;
typedef     vector             vint;
typedef     vector              vll;
typedef     vector             vull;
typedef     set                sint;
typedef     set                sull;

const int maxn = 10000 + 5;
vint G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack S;
int a[maxn];

void init(int n) {
    for (int i = 0; i <= n; i++) G[i].clear();
}

void dfs(int u) {
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (!pre[v]) {
            dfs(v);
            lowlink[u] = min(lowlink[u],lowlink[v]);
        }
        else if (!sccno[v]) {
            lowlink[u] = min(lowlink[u],pre[v]);
        }
    }
    if (lowlink[u] == pre[u]) {
        scc_cnt++;
        while (1) {
            int x = S.top();
            S.pop();
            sccno[x] = scc_cnt;
            if (x == u) break;
        }
    }
}

void find_scc(int n) {
    dfs_clock = scc_cnt = 0;
    CLR(sccno,0);
    CLR(pre,0);
    for (int i = 1; i <= n; i++) {
        if (!pre[i]) dfs(i);
    }
}

int cnt[maxn];

int main() {
    int n,m;
    //freopen("data.in","r",stdin);
    while (cin>>n>>m) {
        init(n);
        for (int i = 0; i < m; i++) {
            int a,b;
            scanf("%d%d",&a,&b);
            G[a].PB(b);
        }
        //for (int i = 0; i <= scc_cnt; i++) new_g[i].clear();
        find_scc(n);
        for (int i = 1; i <= n; i++) {
            cnt[sccno[i]]++;
        }
        int ans = 0;
        /*cout< 1) ans++;
        }
        cout<


你可能感兴趣的:(强连通)