POJ 3180

The Cow Prom










Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1132   Accepted: 713

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


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

强连通分量

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #include <stack>
6
7 using namespace std;
8
9 const int MAX_N = 10005;
10 const int edge = 50005;
11 int N,M;
12 int first[MAX_N],Next[edge],v[edge];
13 int low[MAX_N],pre[MAX_N],cmp[MAX_N];
14 int num[MAX_N];
15 int dfs_clock,scc_cnt;
16 stack<int> S;
17
18
19 void add_edge(int id,int u) {
20 int e = first[u];
21 Next[id] = e;
22 first[u] = id;
23 }
24
25 void dfs(int u) {
26 pre[u] = low[u] = ++dfs_clock;
27 S.push(u);
28 for(int e = first[u]; e != -1; e = Next[e]) {
29 if(!pre[ v[e] ]) {
30 dfs( v[e] );
31 low[u] = min(low[u],low[ v[e] ]);
32 } else if(!cmp[ v[e] ]) {
33 low[u] = min(low[u],pre[ v[e] ]);
34 }
35 }
36
37 if(low[u] == pre[u]) {
38 ++scc_cnt;
39 for(;;) {
40 int x = S.top(); S.pop();
41 cmp[x] = scc_cnt;
42 num[scc_cnt]++;
43 if(x == u) break;
44 }
45 }
46 }
47
48 void scc() {
49 dfs_clock = scc_cnt = 0;
50 memset(cmp,0,sizeof(cmp));
51 memset(pre,0,sizeof(pre));
52
53 for(int i = 1; i <= N; ++i) if(!pre[i]) dfs(i);
54 }
55
56 void solve() {
57 int ans = 0;
58 scc();
59 for(int i = 1; i <= scc_cnt; ++i) {
60 if(num[i] >= 2) ++ans;
61 }
62
63 printf("%d\n",ans);
64 }
65 int main()
66 {
67 、、freopen("sw.in","r",stdin);
68 scanf("%d%d",&N,&M);
69 for(int i = 1; i <= N; ++i) first[i] = -1;
70 for(int i = 0; i < M; ++i) {
71 int u;
72 scanf("%d%d",&u,&v[i]);
73 add_edge(i,u);
74 }
75
76 solve();
77 return 0;
78 }

POJ 3180,布布扣,bubuko.com

时间: 2024-08-27 00:02:22

POJ 3180的相关文章

poj 3180 The Cow Prom 强连通分量

水题,直接贴代码. //poj 3180 //sep9 #include <iostream> #include <stack> using namespace std; const int maxN=10024; const int maxM=50048; int sum[maxN]; int head[maxN],dfn[maxN],low[maxN],ins[maxN],belong[maxN]; stack<int> s; struct Edge{ int v,

POJ 3180 The Cow Prom

传送门:http://poj.org/problem?id=3180 解题思路: #include <cstdio> #include <algorithm> #include <cstring> #include <map> #include <iostream> using namespace std; const int MAXN=10010; const int MAXM=50010; struct Edge{ int to; int n

poj 3180 The Cow Prom(tarjan+缩点 easy)

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

POJ 3180 The Cow Prom(强联通)

题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好,顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.为了跳这种圆舞,她们找了M(2≤M≤50000)条绳索.若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛.有的奶牛可能握有很多绳索,也有的奶牛可能一

POJ 3180 The cow Prom Tarjan基础题

题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stack> 6 #define M 50010 7 #define N 10010 8 using namespace std; 9 int n,m,u,v,head[

连通图专题汇总

联通图课件 建议学习过程,先简单看了解课件,然后看模版代码,上面会有详细注释,以及我第一做这些代码时候遇到的问题. 无向连通图求 桥和割点 割点:无向连通图中,如果删除某点后,图变成不连通了,则称该点为割点. 桥:   无向连通图中,如果删除某条边后,图变成不连通了,则该边为桥. 注:桥和割点是无向图中的概念. 求桥: 在求割点的基础上吗,假如一个边没有重边(重边 1-2, 1->2 有两次,那么 1->2 就是有两条边了,那么 1->2就不算是桥了). 当且仅当 (u,v) 为父子边,

Book---强连通分量

这几天一直在做强连通,现在总结一小下 1.定义 在一个有向图中,如果任意的两个点都是相互可达的,就说这个图是强连通的,有向图的极大强连通子图,称为强连通分量 2.求法 学的是白书上的tarjan算法 用到了DFS的时间戳 假设一个强连通分量C,其中的第一个点是 P,那么DFS下去,就一定能够找到一个K点,返回P点,这条DFS路径上的点就处于这个强连通分量C中 假如现在发现节点v,同时发现节点v最远只能够到达节点u,那么节点u就是这个强连通分量最先被发现的节点 这样就转化成了求一个点u最远能够到达

连通图练习总结

连通图是图论基于联通的一个概念,在ACM中针对图论的考察一部分是也是基于连通图.针对这类问题的解题基本思路就是先求出对应的连通分量(有向图的强连通,无向图的双连通)对图进行简化,然后再结合其他算法计算. 1. POJ 3180 The Cow Prom 这个题如果能理解题目的话,怎么做就很明显了,能形成一个可以转圈的小群,就相当于一个强连通分量,需要注意的就是这个小群不可以只有一头牛. #include <set> #include <map> #include <list&

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i