POJ-2762 Going from u to v or from v to u? 双连通分量+拓扑排序

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

  判断在一个有向图中,是否任意的两点存在一条通路。

  首先用tarjan算法进行边-双连通分量缩点,接下来就是判断树的分支只有一个,那么就用拓扑排序每次判断入度为0的点是否只有一个。

  1 //STATUS:C++_AC_360MS_340KB

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

 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*6],e2[N*6];

 57 int first[N],next[N*6],first2[N],next2[N*6],pre[N],sccno[N],low[N],c[N],vis[N],p[N];

 58 int T,n,m,mt,dfs_clock,scnt,mt2;

 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 adde2(int a,int b)

 70 {

 71     e2[mt2].u=a;e2[mt2].v=b;

 72     next2[mt2]=first2[a],first2[a]=mt2++;

 73 }

 74 

 75 void dfs(int u)

 76 {

 77     int i,j,v;

 78     pre[u]=low[u]=++dfs_clock;

 79     s.push(u);

 80     for(i=first[u];i!=-1;i=next[i]){

 81         v=e[i].v;

 82         if(!pre[v]){

 83             dfs(v);

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

 85         }

 86         else if(!sccno[v]){

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

 88         }

 89     }

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

 91         int x=-1;

 92         scnt++;

 93         while(x!=u){

 94             x=s.top();s.pop();

 95             sccno[x]=scnt;

 96         }

 97     }

 98 }

 99 

100 int topo()

101 {

102     int i,j,cnt,sum=scnt,w;

103     mem(c,0);

104     for(;sum;sum--){

105         mem(vis,0);

106         for(i=1;i<=scnt;i++){

107             for(j=first2[i];j!=-1;j=next2[j]){

108                 vis[e2[j].v]=1;

109             }

110         }

111         cnt=0;

112         for(i=1;i<=scnt;i++){

113             if(!c[i] && !vis[i]){w=i;cnt++;}

114             if(cnt>=2)return 0;

115         }

116         first2[w]=-1;

117         c[w]=1;

118     }

119     return 1;

120 }

121 

122 int main()

123 {

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

125     int i,j,a,b,x,y,ok;

126     scanf("%d",&T);

127     while(T--)

128     {

129         scanf("%d%d",&n,&m);

130         mem(first,-1);mt=0;

131         for(i=1;i<=n;i++)p[i]=i;

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

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

134             x=find(a);y=find(b);

135             if(x!=y)p[y]=p[x];

136             adde(a,b);

137         }

138         ok=0;

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

140             if(p[i]==i)ok++;

141             if(ok>=2){ok=0;break;}

142         }

143         if(ok==1){

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

145             scnt=dfs_clock=0;

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

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

148             }

149             if(scnt!=1){

150                 mt2=0;

151                 mem(first2,-1);

152                 for(i=0;i<mt;i++){

153                     if(sccno[e[i].u]!=sccno[e[i].v]){

154                         adde2(sccno[e[i].u],sccno[e[i].v]);

155                     }

156                 }

157                 ok=topo();

158             }

159         }

160 

161         printf("%s\n",ok?"Yes":"No");

162     }

163     return 0;

164 }

 

你可能感兴趣的:(poj)