POJ3180-The Cow Prom

来源:互联网 发布:种族灭绝政策 知乎 编辑:程序博客网 时间:2024/04/29 14:59

The Cow Prom
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2234 Accepted: 1322

Description

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 42 43 51 24 1

Sample Output

1

Hint

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.

Source

USACO 2006 January Silver

题意:有n头牛跳舞,顺时针顺序由1到n编号.每只奶牛都面对水池,有m条绳索。若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛。有的奶牛可能握有很多绳索,也有的奶牛可能一条绳索都没有握。判断一头牛舞跳得是否成功可以这样检验:沿着她牵引的绳索,找到她牵引的奶牛,再沿着这只奶牛牵引的绳索,又找到一只被牵引的奶牛,如此下去,若最终能回到原位,则她的圆舞跳得成功。如果不能回到原位,那她的圆舞是不成功的。如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合。单独的一头牛跳舞不算成功。问成功跳了圆舞的奶牛有多少个组。其实就是求结点数大于等于2的强联通分量的个数



#include <iostream>    #include <cstdio>    #include <string>    #include <cstring>    #include <algorithm>    #include <cmath>    #include <vector>    #include <map>    #include <set>    #include <queue>    #include <stack>    #include <functional>    #include <climits>    using namespace std;#define LL long long    const int INF = 0x3f3f3f3f;const int N = 10100;int n, m;struct Node{int v, nt;} edge[N * 100];int s[N], cnt;int dfn[N], low[N], id[N], vis[N], dep;bool instack[N];int in[N];int res;stack<int>st;void AddEdge(int u, int v){edge[cnt].v = v;edge[cnt].nt = s[u];s[u] = cnt++;}void tarjan(int u){st.push(u);instack[u] = true;vis[u] = true;dfn[u] = low[u] = ++dep;for (int i = s[u]; ~i; i = edge[i].nt){int v = edge[i].v;if (!vis[v])  // 生成树的边.    {tarjan(v);low[u] = min(low[u], low[v]);}else if (instack[v])//在栈中,回边.    low[u] = min(low[u], dfn[v]);}if (dfn[u] == low[u])//顶点u为根的子树是一个强连同块    {int t;do{id[t = st.top()] = res;st.pop();instack[t] = false; //low[t] = n;    } while (t != u);res++;//强连通分量增加    }}void solve(){res = 0, dep = 0;while (!st.empty()) st.pop();memset(vis, 0, sizeof vis);memset(instack, 0, sizeof instack);for (int i = 1; i <= n; i++)if (!vis[i]) tarjan(i);// Debug    /* for(int i = 1; i <= n; i++)printf("dfn[%d] = %d, low[%d] = %d\n", i,dfn[i], i,low[i]);for(int i = 1; i <= n; i++)printf("id[%d] = %d\n", i, id[i] );*/memset(vis, 0, sizeof vis);for (int i = 1; i <= n; i++)vis[id[i]]++;int sum = 0;for (int i = 0; i < res; i++)if (vis[i] > 1) sum++;printf("%d\n", sum);}int main(){while (~scanf("%d%d", &n, &m)){memset(s, -1, sizeof s);cnt = 0;for (int i = 0; i<m; i++){int u, v;scanf("%d%d", &u, &v);AddEdge(u, v);}solve();}return 0;}