POJ-2186 Popular Cows 强连通分量

  题目链接:http://poj.org/problem?id=2186

  求出度为0的强连通分量的点的个数。

  1 //STATUS:C++_AC_94MS_920KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //define
 25 #define pii pair<int,int>
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define lson l,mid,rt<<1
 28 #define rson mid+1,r,rt<<1|1
 29 #define PI acos(-1.0)
 30 //typedef
 31 typedef __int64 LL;
 32 typedef unsigned __int64 ULL;
 33 //const
 34 const int N=10010;
 35 const int INF=0x3f3f3f3f;
 36 const int MOD=100000,STA=8000010;
 37 const LL LNF=1LL<<60;
 38 const double EPS=1e-8;
 39 const double OO=1e15;
 40 const int dx[4]={-1,0,1,0};
 41 const int dy[4]={0,1,0,-1};
 42 //Daily Use ...
 43 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 46 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 47 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 52 //End
 53 
 54 struct Edge{
 55     int u,v;
 56 }e[N*5];
 57 int first[N],next[N*5],pre[N],sccno[N],low[N],vis[N],p[N];
 58 int n,m,mt,dfs_clock,scnt;
 59 stack<int> s;
 60 
 61 int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
 62 
 63 void adde(int a,int b)
 64 {
 65     e[mt].u=a;e[mt].v=b;
 66     next[mt]=first[a],first[a]=mt++;
 67 }
 68 
 69 void dfs(int u)
 70 {
 71     int i,j,v;
 72     pre[u]=low[u]=++dfs_clock;
 73     s.push(u);
 74     for(i=first[u];i!=-1;i=next[i]){
 75         v=e[i].v;
 76         if(!pre[v]){
 77             dfs(v);
 78             low[u]=Min(low[u],low[v]);
 79         }
 80         else if(!sccno[v]){
 81             low[u]=Min(low[u],low[v]);
 82         }
 83     }
 84     if(low[u]==pre[u]){
 85         int x=-1;
 86         scnt++;
 87         while(x!=u){
 88             x=s.top();s.pop();
 89             sccno[x]=scnt;
 90         }
 91     }
 92 }
 93 
 94 int main()
 95 {
 96  //   freopen("in.txt","r",stdin);
 97     int i,j,a,b,ans,x,y,ok;
 98     while(~scanf("%d%d",&n,&m))
 99     {
100         mem(first,-1);mt=0;
101         for(i=1;i<=n;i++)p[i]=i;
102         for(i=0;i<m;i++){
103             scanf("%d%d",&a,&b);
104             x=find(a);y=find(b);
105             if(x!=y)p[y]=p[x];
106             adde(a,b);
107         }
108         ok=0;
109         for(i=1;i<=n;i++){
110             if(p[i]==i)ok++;
111             if(ok>=2)break;
112         }
113         ans=0;
114         if(ok==1){
115             mem(pre,0);mem(sccno,0);
116             scnt=dfs_clock=0;
117             for(i=1;i<=n;i++){
118                 if(!pre[i])dfs(i);
119             }
120             for(i=1;i<=scnt;i++)vis[i]=1;
121             for(i=0;i<mt;i++){
122                 if(sccno[e[i].u]!=sccno[e[i].v]){
123                     vis[sccno[e[i].u]]=0;
124                 }
125             }
126             ok=0;
127             for(i=1;i<=scnt;i++){
128                 ok+=vis[i];
129                 if(ok>=2){ok=0;break;}
130             }
131             if(ok){
132                 for(i=1;i<=n;i++){
133                     if(vis[sccno[i]])ans++;
134                 }
135             }
136         }
137 
138         printf("%d\n",ans);
139     }
140     return 0;
141 }

 

posted @ 2013-05-21 11:00  zhsl  阅读(200)  评论(0编辑  收藏  举报