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

### Analysis

``` 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #define maxn 10000+10
6 #define maxm 50000+10
7 using namespace std;
9 {
10     int x=0;
11     bool f=1;
12     char c=getchar();
13     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
14     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
15     if(f) return x;
16     return 0-x;
17 }
18 inline void write(int x)
19 {
20     if(x<0){putchar('-');x=-x;}
21     if(x>9)write(x/10);
22     putchar(x%10+'0');
23 }
24 int n,m,cnt,num,top,col,ans;
26 struct node{int to,next;}edge[maxm];
27 inline void add(int x,int y)
28 {
29     edge[++cnt].to=y;
32 }
33 inline void Tarjan(int u)
34 {
35     dfn[u]=low[u]=++num;
36     st[++top]=u;
38     {
39         int v=edge[i].to;
40         if(!dfn[v])
41         {
42             Tarjan(v);
43             low[u]=min(low[u],low[v]);
44         }
45         else if(!co[v])
46             low[u]=min(low[u],dfn[v]);
47     }
48     if(low[u]==dfn[u])
49     {
50         co[u]=++col;
51         while(st[top]!=u)
52         {
53             si[col]++;
54             co[st[top]]++;
55             --top;
56         }
57         si[col]++;
58         --top;
59     }
60 }
61 int main()
62 {
64     for(int i=1;i<=m;i++)
65     {
68     }
69     for(int i=1;i<=n;i++)
70         if(!dfn[i])
71             Tarjan(i);
72     for(int i=1;i<=col;i++)
73         if(si[i]>1) ans++;
74     write(ans);
75     return 0;
76 }```