1654: [Usaco2006 jan]the Cow Prom Dairy Ball

Source: Internet
Author: User
Tags min time limit
1654: [Usaco2006 jan]the Cow prom Dairy BallTime Limit:5 Sec Memory limit:64 MB
submit:287 solved:215
[Submit] [Status] [Discuss] 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 pulled the entire group counterclockwise (anti-clockwise in Bri Tish 中文版). 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.      John's N (2≤n≤10000) cows were very excited because it was prom night. They wear dresses and new shoes, not flowers, they want to perform a round dance.      only cows can perform this round dance. The round dance needs some ropes and a round pool. The cows were standing around the pool, numbered 1 to n in the clockwise order. Every cow is faced with a pool so she can see every other cow. In order to dance this round, they found the M (2≤m≤50000) rope. On the hoof of several cows, one end of the rope is held, the rope bypasses the pool clockwise, and the other end is tied to another cow. In this way, some cows can pull other cows. Some cows may hold a lot of ropes, and some cows may not have a rope for a cow, such as Bessie, her round dance success, can be tested: along the ropes she pulled, find her traction cows, and then along the rope traction of the cow, and found a tractor-driven cows, so down, If she could eventually return to Bessie, her round dance would have been successful, as the cows on the ring could be pulled counterclockwise to jump to the whirling dance. If such a test cannot be completed, then her round dance is unsuccessful.      if two of the successful round-dancing cows are connected by ropes, they can belong to a combination.      give a description of each rope, find out how many combinations of cows have successfully danced round.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 CL     Ockwise direction. Line 1th enters N and M, and the next M-line is two integers a and B, indicating a traction B. Output

* Line 1: A single line with A single integer This is the number of groups successfully dancing the Round Dance. Number of cow combinations that successfully danced round. Sample Input 5 4
2 4
3 5
1 2
4 1

INPUT DETAILS:

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____//
\_______/

Sample Output 1

HINT

1,2,4 These three cows belong to a successful dance-dancing combination. and 3, 52 cows did not jump the successful round dance
Source

Silver



The first can be a circle of some cows to make a ring, and then can jump round the cows belong to a combination, then, an SCC is a combination. Tarjan for SCC, note that SCC elements need to be >1
attached code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> #include < cmath> #include <cstring> #include <string> #include <fstream> #include <climits> #include <queue> #include <map> #include <set> #include <stack> #define M 100010 #define N 10001 using NAMESP
Ace STD;
int N,m,ans;
int head[n],pos;
int A, B;
stack<int>s; struct Edge{int to,next;}
E[M];
void Add (int a,int b) {pos++;e[pos].to=b,e[pos].next=head[a],head[a]=pos;} int dfn[n],low[n],cnt,id;
BOOL In[n]; void Tarjan (int u) {dfn[u]=low[u]=++id;
	S.push (u); in[u]=1;
			for (int i=head[u];i;i=e[i].next) {int v=e[i].to;
			if (In[v]) low[u]=min (Low[u],dfn[v]);
					else {Tarjan (v);
				Low[u]=min (Low[u],low[v]);
			}} if (Low[u]==dfn[u]) {int v,num=0;cnt++; do {v=s.top ();
					S.pop ();
				in[v]=0;num++;
			}while (V!=u);
		if (num!=1) ans++;
	}} int main () {scanf ("%d%d", &n,&m);
		for (int i=1;i<=m;i++){scanf ("%d%d", &a,&b);
		Add (A, b);
	} for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i);
printf ("%d\n", ans);
 }


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.