hdu - 1269 迷宫城堡 (强连通裸题)

http://acm.hdu.edu.cn/showproblem.php?pid=1269

判断一个图是不是强连通,缩点之后判断顶点数是不是为1即可.

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <string>
  8 #include <set>
  9 #include <functional>
 10 #include <numeric>
 11 #include <sstream>
 12 #include <stack>
 13 #include <map>
 14 #include <queue>
 15 
 16 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 17 
 18 #define ll long long
 19 #define inf 0x7f7f7f7f
 20 #define lc l,m,rt<<1
 21 #define rc m + 1,r,rt<<1|1
 22 #define pi acos(-1.0)
 23 
 24 #define L(x)    (x) << 1
 25 #define R(x)    (x) << 1 | 1
 26 #define MID(l, r)   (l + r) >> 1
 27 #define Min(x, y)   (x) < (y) ? (x) : (y)
 28 #define Max(x, y)   (x) < (y) ? (y) : (x)
 29 #define E(x)        (1 << (x))
 30 #define iabs(x)     (x) < 0 ? -(x) : (x)
 31 #define OUT(x)  printf("%I64d\n", x)
 32 #define lowbit(x)   (x)&(-x)
 33 #define Read()  freopen("a.txt", "r", stdin)
 34 #define Write() freopen("dout.txt", "w", stdout);
 35 
 36 using namespace std;
 37 #define N 10100
 38 //N为最大点数
 39 #define M 100100
 40 //M为最大边数
 41 int n, m;//n m 为点数和边数
 42 
 43 struct Edge{
 44     int from, to, nex;
 45     bool sign;//是否为桥
 46 }edge[M<<1];
 47 int head[N], edgenum;
 48 void add(int u, int v){//边的起点和终点
 49     Edge E={u, v, head[u], false};
 50     edge[edgenum] = E;
 51     head[u] = edgenum++;
 52 }
 53 
 54 int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
 55 int taj;//连通分支标号,从1开始
 56 int Belong[N];//Belong[i] 表示i点属于的连通分支
 57 bool Instack[N];
 58 vector<int> bcc[N]; //标号从1开始
 59 
 60 void tarjan(int u ,int fa){
 61     DFN[u] = Low[u] = ++ Time ;
 62     Stack[top ++ ] = u ;
 63     Instack[u] = 1 ;
 64 
 65     for (int i = head[u] ; ~i ; i = edge[i].nex ){
 66         int v = edge[i].to ;
 67         if(DFN[v] == -1)
 68         {
 69             tarjan(v , u) ;
 70             Low[u] = min(Low[u] ,Low[v]) ;
 71             if(DFN[u] < Low[v])
 72             {
 73                 edge[i].sign = 1;//为割桥
 74             }
 75         }
 76         else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;
 77     }
 78     if(Low[u] == DFN[u]){
 79         int now;
 80         taj ++ ; bcc[taj].clear();
 81         do{
 82             now = Stack[-- top] ;
 83             Instack[now] = 0 ;
 84             Belong [now] = taj ;
 85             bcc[taj].push_back(now);
 86         }while(now != u) ;
 87     }
 88 }
 89 
 90 void tarjan_init(int all){
 91     memset(DFN, -1, sizeof(DFN));
 92     memset(Instack, 0, sizeof(Instack));
 93     top = Time = taj = 0;
 94     for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意开始点标!!!
 95 }
 96 vector<int>G[N];
 97 int du[N];
 98 void suodian(){
 99     memset(du, 0, sizeof(du));
100     for(int i = 1; i <= taj; i++)G[i].clear();
101     for(int i = 0; i < edgenum; i++){
102         int u = Belong[edge[i].from], v = Belong[edge[i].to];
103         if(u!=v)
104         {
105             G[u].push_back(v), du[v]++;
106             //printf("%d %d\n",u,v);
107         }
108     }
109 }
110 void init(){memset(head, -1, sizeof(head)); edgenum=0;}
111 
112 int main()
113 {
114    // Read();
115     int a,b;
116     while(~scanf("%d%d",&n,&m)&&n+m)
117     {
118         init();
119         for(int i=0;i<m;i++)
120         {
121             scanf("%d%d",&a,&b);
122             add(a,b);
123         }
124         tarjan_init(n);
125         if(taj==1) puts("Yes");
126         else puts("No");
127         //suodian();
128     }
129     return 0;
130 }

 

posted @ 2015-08-14 09:46  NowAndForever  阅读(159)  评论(0编辑  收藏  举报