1. 程式人生 > >The Cow Prom POJ - 3180 (Tarjan求強連通分量)

The Cow Prom POJ - 3180 (Tarjan求強連通分量)

The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. 

Only cows can perform the Round Dance which requires a set of ropes and a circular stock tank. To begin, the cows line up around a circular stock tank and number themselves in clockwise order consecutively from 1..N. Each cow faces the tank so she can see the other dancers. 

They then acquire a total of M (2 <= M <= 50,000) ropes all of which are distributed to the cows who hold them in their hooves. Each cow hopes to be given one or more ropes to hold in both her left and right hooves; some cows might be disappointed. 

For the Round Dance to succeed for any given cow (say, Bessie), the ropes that she holds must be configured just right. To know if Bessie's dance is successful, one must examine the set of cows holding the other ends of her ropes (if she has any), along with the cows holding the other ends of any ropes they hold, etc. When Bessie dances clockwise around the tank, she must instantly pull all the other cows in her group around clockwise, too. Likewise, 
if she dances the other way, she must instantly pull the entire group counterclockwise (anti-clockwise in British English). 

Of course, if the ropes are not properly distributed then a set of cows might not form a proper dance group and thus can not succeed at the Round Dance. One way this happens is when only one rope connects two cows. One cow could pull the other in one direction, but could not pull the other direction (since pushing ropes is well-known to be fruitless). Note that the cows must Dance in lock-step: a dangling cow (perhaps with just one rope) that is eventually pulled along disqualifies a group from properly performing the Round Dance since she is not immediately pulled into lockstep with the rest. 

Given the ropes and their distribution to cows, how many groups of cows can properly perform the Round Dance? Note that a set of ropes and cows might wrap many times around the stock tank.

Input

Line 1: Two space-separated integers: N and M 

Lines 2..M+1: Each line contains two space-separated integers A and B that describe a rope from cow A to cow B in the clockwise direction.

Output

Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.

Sample Input

5 4
2 4
3 5
1 2
4 1

Sample Output

1

題目意思:

給出n個點,m條邊,問有幾個包含節點個數大於1的強連通分量。

思路:

就是一個最裸的求強連通分量的模板。。。

直接上程式碼了,裡面有些許註釋。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 5 * 1e4 + 100;

int n, m, top, tot, num, sum;
int head[maxn], dfn[maxn], low[maxn], Stack[maxn], color[maxn];

struct node {
    int v, next;
}edge[maxn];

inline void add(int u, int v) {
    edge[tot].v = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

inline void Init() {
    tot = 0;          //用於鏈式前向星存圖
    top = 0;          //用於棧
    num = 0;          //用於dfs求dfn陣列
    sum = 0;          //強連通分量的數量
    memset(head, -1, sizeof(head));
}

int ans;

void Tarjan(int u) {
    dfn[u] = low[u] = ++ num;
    Stack[++top] = u;     //將當前節點壓入棧中
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if(!dfn[v]) {
            Tarjan(v);
            low[u] = min(low[v], low[u]);
        }
        else if(color[v] == 0) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u]) {     //可以構成強連通分量
        color[u] = ++sum;      //記錄每個節點所屬的強連通分量
        int cnt = 1;          //開始統計節點數目
        while(Stack[top] != u) {
            color[Stack[top--]] = sum;
            cnt ++;
        }
        top --;
        if(cnt > 1)      //大於1才加上
            ans ++;
    }
}

int main()
{
    cin >> n >> m;
    Init();
    int u, v;
    for(int i = 1; i <= m; ++ i)
    {
        scanf("%d%d", &u, &v);
        add(u, v);
    }
    ans = 0;
    for(int i = 1; i <= n; ++ i) {        //它可能不是連通圖
        if(!dfn[i]) {
            Tarjan(i);
        }
    }
    cout << ans << endl;
    return 0;
}