1. 程式人生 > >P2863 [USACO06JAN]牛的舞會The Cow Prom(tarjan演算法求強連通)

P2863 [USACO06JAN]牛的舞會The Cow Prom(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.

約翰的N (2 <= N <= 10,000)只奶牛非常興奮,因為這是舞會之夜!她們穿上禮服和新鞋子,別 上鮮花,她們要表演圓舞.

只有奶牛才能表演這種圓舞.圓舞需要一些繩索和一個圓形的水池.奶牛們圍在池邊站好, 順時針順序由1到N編號.每隻奶牛都面對水池,這樣她就能看到其他的每一隻奶牛.

為了跳這種圓舞,她們找了 M(2<M< 50000)條繩索.若干只奶牛的蹄上握著繩索的一端, 繩索沿順時針方繞過水池,另一端則捆在另一些奶牛身上.這樣,一些奶牛就可以牽引另一些奶 牛.有的奶牛可能握有很多繩索,也有的奶牛可能一條繩索都沒有.

對於一隻奶牛,比如說貝茜,她的圓舞跳得是否成功,可以這樣檢驗:沿著她牽引的繩索, 找到她牽引的奶牛,再沿著這隻奶牛牽引的繩索,又找到一隻被牽引的奶牛,如此下去,若最終 能回到貝茜,則她的圓舞跳得成功,因為這一個環上的奶牛可以逆時針牽引而跳起旋轉的圓舞. 如果這樣的檢驗無法完成,那她的圓舞是不成功的.

如果兩隻成功跳圓舞的奶牛有繩索相連,那她們可以同屬一個組合.

給出每一條繩索的描述,請找出,成功跳了圓舞的奶牛有多少個組合?

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 …

輸入輸出格式

輸入格式:

 

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.

 

輸出格式:

 

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

 

輸入輸出樣例

輸入樣例#1: 複製

5 4
2 4
3 5
1 2
4 1

輸出樣例#1: 複製

1

說明

Explanation of the sample:

ASCII art for Round Dancing is challenging. Nevertheless, here is a representation of the cows around the stock tank:

       _1___
      /**** \
   5 /****** 2
  / /**TANK**|
  \ \********/
   \ \******/  3
    \ 4____/  /
     \_______/

Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don't have the second rope they'd need to be able to pull both ways, thus they can not properly perform the Round Dance.

 

#include<iostream>
using namespace std;
const int maxn = 50010;
int head[maxn], num[maxn], dfn[maxn], low[maxn], Stack[maxn], vis[maxn];
int n, m, cnt = 1, a, b, total, sum = 1, index;
struct node {
    int to;
    int next;
}edge[2 * maxn];

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

void tarjan(int u) {
    dfn[u] = low[u] = ++total;
    vis[u] = 1;
    Stack[++index] = u;
    for (int i = head[u]; i != 0; i = edge[i].next) {
        int v = edge[i].to;
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (vis[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (low[u] == dfn[u]) {
        sum++;
        do {
            vis[Stack[index--]] = 0;
            num[sum]++;
        } while (Stack[index + 1] != u);
    }
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        add(a, b);
    }
    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) {
            tarjan(i);
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (num[i] > 1) ans++;
    }
    cout << ans << endl;
    return 0;
}