首页 > 代码库 > HDU 1269:迷宫城堡(强连通)

HDU 1269:迷宫城堡(强连通)

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

题意:确定是否是一个强连通图。

思路:裸的tarjan算法。

 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 #include <string> 6 #include <cmath> 7 #include <queue> 8 #include <vector> 9 #include <stack>10 using namespace std;11 #define M 10001012 #define N 1001013 struct node14 {15     int v, next;16 }edge[M];17 int n, m, tot, cnt, head[N];18 int dfn[N], low[N];19 bool vis[N];20 stack<int> sta;21 22 void init()23 {24     memset(dfn, 0, sizeof(dfn));25     memset(low, 0, sizeof(low));26     memset(head, -1, sizeof(head));27     memset(vis, false, sizeof(vis));28     tot = cnt = 0;29     while(!sta.empty()) sta.pop();30 }31 32 void add(int u, int v)33 {34     edge[tot].next = head[u]; edge[tot].v = v; head[u] = tot++;35 }36 37 int tarjan(int u)38 {39     dfn[u] = low[u] = ++cnt;40     sta.push(u);41     vis[u] = true;42     for(int i = head[u]; ~i; i = edge[i].next) {43         int v = edge[i].v;44         if(!dfn[v]) {45             tarjan(v);46             if(low[v] < low[u]) low[u] = low[v];47         } else {48             if(dfn[v] < low[u] && vis[v]) low[u] = dfn[v];49         }50     }51     if(low[u] == dfn[u]) {52         int top = 0;53         int ans = 0;54         while(top != u) { //注意一开始top等于u的情况这样ans会只算0,而不是155             ans++;56             top = sta.top(); sta.pop();57             vis[top] = false;58         }59         return ans;60     }61 }62 63 int main()64 {65     while(scanf("%d%d", &n, &m)) {66         if(n + m == 0) break;67         init();68         for(int i = 0; i < m; i++) {69             int u, v;70             scanf("%d%d", &u, &v);71             add(u, v);72         }73         int ans = tarjan(1);74         if(n == ans) puts("Yes");75         else puts("No");76     }77     return 0;78 }

 

HDU 1269:迷宫城堡(强连通)