1. 程式人生 > >luogu P2863 [USACO06JAN]牛的舞會The Cow Prom

luogu P2863 [USACO06JAN]牛的舞會The Cow Prom

urn rcu pac fine num into see 檢驗 holding

題目描述

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>
#include <cstdio>
#include <algorithm>
#include <stack>

using namespace std;
const int N = 5e4 + 10;
const int M = N << 1;

int low[N], dfn[N], head[N];
int n, m, tmp_js, now = 1, dfs_clock, answer;
bool vis[N];
struct Node{
	int u, v, nxt;
}E[M];
stack <int> S;

inline int read()
{
	int x = 0; char c = getchar();
	while(c < ‘0‘ || c > ‘9‘)c = getchar();
	while(c >= ‘0‘ && c <= ‘9‘)x = x * 10 + c - ‘0‘, c = getchar();
	return x;
}

inline void add(int u, int v)
{
	E[now].u = u;
	E[now].v = v;
	E[now].nxt = head[u];
	head[u] = now ++;
}

void Tarjan(int x)
{
	low[x] = dfn[x] = ++ dfs_clock;
	S.push(x);
	vis[x] = 1;
	for(int i = head[x]; ~ i; i = E[i].nxt)
	{
		int v = E[i].v;
		if(!dfn[v])
		{
			Tarjan(v);
			low[x] = min(low[x], low[v]);
		}
		else if(vis[v])
		{
			low[x] = min(low[x], dfn[v]);
		}
	}
	if(low[x] == dfn[x])
	{
		tmp_js = 0;
		while(x != S.top())
		{
			tmp_js ++;
			vis[S.top()] = 0;
			S.pop();
		}
		tmp_js ++;
		vis[S.top()] = 0;
		S.pop();
		if(tmp_js > 1)
			answer ++;
	}
}

int main()
{
	n = read();
	m = read();
	for(int i = 1; i <= n; i ++)
		head[i] = -1;
	for(int i = 1; i <= m; i ++)
	{
		int u = read();
		int v = read();
		add(u, v);
		//add(v, u);
	}
	for(int i = 1; i <= n; i ++)
		if(!dfn[i])
			Tarjan(i);
	printf("%d", answer);
	return 0;
} 

  

luogu P2863 [USACO06JAN]牛的舞會The Cow Prom