POJ3180 (number of nodes >=2 of the strongly connected component of the graph)

Source: Internet
Author: User

The Cow Prom
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1451 Accepted: 922

Description

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

Only cows can perform the Round Dance which requires a set of ropes and a circular the stock tank. To begin, the cows line up around a circular the stock tank and number themselves on 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 is distributed to the cows who hold them in th Eir hooves. Each cow hopes to is given one or more ropes to hold in both she left and right hooves; Some cows might be disappointed.

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

Of course, if the ropes is not properly distributed then a set of cows might not form a proper dance group and thus can n OT succeed at the Round Dance. The one-to-one-happens is while only one rope connects and cows. One cow could pulling the other in one direction, but could not pulling the other direction (since pushing ropes is well-known t o be fruitless). Note that the cows must Dance in lock-step:a dangling cow (perhaps with just one rope) that's eventually pulled along di Squalifies a group from properly performing the Round Dance since she's not immediately pulled to lockstep with the Res T.

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, space-separated integers a and B that describe a rope from cow A to cow B in the Cloc Kwise direction.

Output

Line 1: A single line with A single integer This 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 is properly connected and form a complete Round Dance group. Cows 3 and 5 don ' t has the second rope they ' d need to being able to pulling both ways, thus they can not properly perform the R Ound Dance.

Source

Usaco 2006 January Silver Typical reading comprehension problem, looked at a long time the problem is to ask the number of strong connected component nodes >=2. Although simple, but the first time to make the wrong number of small places, to hit the code must concentrate, the following list of errors
/*id:linkarftcprog:3180.cpplang:c++*/#include<map>#include<Set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<string>#include<utility>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineRandin Srand ((unsigned int) time (NULL))#defineInput freopen ("Input.txt", "R", stdin)#defineDebug (s) cout << "s =" << s << endl;#defineOutstars cout << "*************" << Endl;Const DoublePI = ACOs (-1.0);Const DoubleE = exp (1.0);Const intINF =0x3f3f3f3f;Const intINF =0x7fffffff; typedefLong Longll;Const intMAXN =10010;Const intMAXM =50010;structEdge {intV, Next;} EDGE[MAXM];intHEAD[MAXN], tot;voidinit () {tot=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintv) {EDGE[TOT].V=v; Edge[tot].next=Head[u]; Head[u]= tot + +;}intN, M;intDFN[MAXN], LOW[MAXN];BOOLINS[MAXN];intSCC, Time;stack<int>St;vector<int>VEC[MAXN];voidTarjan (intu) {intv; Dfn[u]= Low[u] = + +Time ;    St.push (U); Ins[u]=true;  for(inti = Head[u]; i +1; i =Edge[i].next) {v=edge[i].v; if(!Dfn[v])            {Tarjan (v); Low[u]=min (Low[v], low[u]); } Else if(Ins[v]) low[u] =min (Low[u], dfn[v]); }    if(Dfn[u] = = Low[u]) {//just started to write dfn[u] = = Low[v]SCC + +;  Do{v=St.top ();            St.pop (); INS[V]=false;        Vec[scc].push_back (v); }  while(U! =v); }}intMain () {//input;    intu, v;  while(~SCANF ("%d%d", &n, &m)) {init ();  for(inti =1; I <= m; i + +) {//I was just beginning to write I<=n.scanf"%d%d", &u, &v);        Addedge (U, v); } memset (DFN,0,sizeof(DFN)); memset (INS,0,sizeof(INS));  while(!st.empty ()) St.pop ();  for(inti =1; I <= N; i + +) vec[i].clear (); SCC=0; time=0;  for(inti =1; I <= N; i + +) {            if(!Dfn[i]) Tarjan (i); }        intAns =0;  for(inti =1; I <= SCC; i + +) {            if(Vec[i].size () >=2) ans + +; } printf ("%d\n", ans); }    return 0;}

POJ3180 (number of nodes >=2 of the strongly connected component of the graph)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.