POJ-2553 The Bottom of a Graph 强连通分量

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

  题意:在有向图中,求出一些点,自己能到达的点一定能到达自己。

  简化模型,就是求出度为0的强连通分量中的那些点。

  1 //STATUS:C++_AC_79MS_712KB

  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=5010;

 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 int pre[N],low[N],sccno[N],ans[N],vis[N];

 55 int n,m,dfs_clock,k,scnt;

 56 vector<int> g[N];

 57 stack<int> s;

 58 

 59 int dfs(int u)

 60 {

 61     pre[u]=++dfs_clock;

 62     int i,v,ok=1;

 63     low[u]=pre[u];

 64     s.push(u);

 65     for(i=0;i<g[u].size();i++){

 66         v=g[u][i];

 67         if(!pre[v]){

 68             dfs(v);

 69             low[u]=Min(low[u],low[v]);

 70         }

 71         else if(!sccno[v]){

 72             low[u]=Min(low[u],low[v]);

 73         }

 74     }

 75     if(low[u]==pre[u]){

 76         scnt++;

 77         int x=-1;

 78         while(x!=u){

 79             x=s.top(),s.pop();

 80             sccno[x]=scnt;

 81         }

 82     //    printf(" %d %d %d\n",u,k,ok);

 83     }

 84     return ok;

 85 }

 86 

 87 int main()

 88 {

 89  //   freopen("in.txt","r",stdin);

 90     int i,j,a,b,ok;

 91     while(~scanf("%d%d",&n,&m) && n)

 92     {

 93         for(i=1;i<=n;i++)g[i].clear();

 94         for(i=0;i<m;i++){

 95             scanf("%d%d",&a,&b);

 96             g[a].push_back(b);

 97         }

 98 

 99         mem(pre,0);mem(sccno,0);

100         dfs_clock=scnt=0;

101         for(i=1;i<=n;i++){

102             if(!pre[i])dfs(i);

103         }

104         for(i=1;i<=scnt;i++)vis[i]=1;

105         for(i=1;i<=n;i++){

106             for(j=0;j<g[i].size();j++){

107                 if(sccno[g[i][j]]!=sccno[i]){

108                     vis[sccno[i]]=0;

109                     break;

110                 }

111             }

112         }

113         k=0;

114         for(i=1;i<=n;i++){

115             if(vis[sccno[i]])ans[k++]=i;

116         }

117         sort(ans,ans+k);

118         printf("%d",ans[0]);

119         for(i=1;i<k;i++)

120             printf(" %d",ans[i]);

121         putchar('\n');

122     }

123     return 0;

124 }

 

你可能感兴趣的:(Graph)