﻿ 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom - 鸿网互联

# 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom

ng 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

ng 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.

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

``` 1 #include<cstdio>
2 #include<cstring>
3 #include<stack>
4 #include<queue>
5 #include<map>
6 #include<stack>
7 #define ls k<<1
8 #define rs k<<1|1
9 #define LL long long
10 using namespace std;
11 const int MAXN=100002;
13 {
14     char c=getchar();int x=0,flag=1;
15     while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}
16     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*flag;
17 }
18 struct node
19 {
20     int u,v,nxt;
21 }edge[MAXN];
23 int num=1;
24 inline void add_edge(int x,int y)
25 {
26     edge[num].u=x;
27     edge[num].v=y;
30 }
31 int low[MAXN];
32 int dfn[MAXN];//时间戳
33 int tot=0;
34 stack<int>s;
35 int ans=0;
36 int vis[MAXN];
37 int color[MAXN],colornum=0;
38 int happen[MAXN];
39 inline void Tarjan(int node)
40 {
41     low[node]=dfn[node]=++tot;
42     vis[node]=1;
43     s.push(node);
45     {
46         if(!dfn[edge[i].v])
47             Tarjan(edge[i].v),low[node]=min(low[node],low[edge[i].v]);
48         else if(vis[edge[i].v])
49             low[node]=min(low[node],dfn[edge[i].v]);
50     }
51     if(dfn[node]==low[node])
52     {
53         colornum++;
54         int h;
55         do
56         {
57             h=s.top();
58             if(color[s.top()]==0)    color[s.top()]=colornum;
59             vis[s.top()]=0;
60             s.pop();
61         }while(node!=h);
62     }
63 }
64 int main()
65 {
68     for(int i=1;i<=m;i++)
69     {
72     }
73     for(int i=1;i<=n;i++)
74         if(color[i]==0)
75             Tarjan(i);
76     for(int i=1;i<=n;i++)
77         happen[color[i]]++;
78     int ans=0;
79     for(int i=1;i<=colornum;i++)
80         if(happen[i]>1)
81             ans++;
82     printf("%d",ans);
83     return 0;
84 }```

<