【强连通】POJ 3180 The Cow Prom

POJ 3180 The Cow Prom 【传送门】

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3029 Accepted: 1743

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 4
2 4
3 5
1 2
4 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

解题思路:

Tarjan板子题……求缩点之后、每个强连通分量中点的个数>1的即可。。

谁想听听这个题的故事……这道题我没看、然后在做其他的题目,结果做了好长时间都WA、然后搜题解……莫名其妙没有看清就看了这个题目的题解,然后我也一直想不通为啥是这个思路、按照这个思路做那个题也WA了好几次……最后发现看错题了,也是醉了。。浪费时间!!

AC代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<set> 
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
#define INF 999999
#define MAXN 100000+5
//struct edge{
//	int to;
//	//int val;
//};
//int next[MAXN]; 
vector<int> G[MAXN];
int dfn[MAXN],low[MAXN];
bool instack[MAXN];
stack<int> s;				
int timing;				//时间戳 
int color[MAXN];		//代表当前结点所属第几个scc 
int colorcnt[MAXN];		//代表第某个scc有几个结点 
int cnt;				//scc个数 
int V,E;				

void init()
{
	for(int i=0;i<=V;i++)
		G[i].clear();
	while(!s.empty())
		s.pop();
	mem(dfn,0);
	mem(color,0);
	mem(colorcnt,0);
	mem(instack,false);
	mem(low,0);
	timing=cnt=0;
}

void tarjan(int u)
{
	timing++;
	dfn[u]=low[u]=timing;
	s.push(u);
	instack[u]=true;
	for(int i=0;i<G[u].size();i++)
	{
		int v=G[u][i];
		if(dfn[v]==0)
		{	
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v])
		{
			low[u]=min(low[u],dfn[v]);
		}
	}
	
	if(low[u]==dfn[u])
	{
		cnt++;
		int temp;
		do
		{
			//记录每个点属于哪个scc
			temp=s.top();
			s.pop();
			instack[temp]=false;
			color[temp]=cnt;
			colorcnt[cnt]++;	
		}while(u!=temp);	
	}
}

int main()
{
	ios::sync_with_stdio(false);
	while(cin >> V >> E && V+E)
	{
		init();
		int a,b,maxn=(-INF);
		for(int i=0;i<E;i++)
		{
			cin >> a >> b;
			G[a].push_back(b);
			maxn=max(maxn,a);
			maxn=max(maxn,b);
		}
		for(int i=1;i<=V;i++)
			if(dfn[i]==0)
				tarjan(i);
		int ans=0;
			for(int i=1;i<=cnt;i++)
				if(colorcnt[i]>1)
					ans++;
			cout << ans << endl;
					
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值