标签:
1 vector<int> G[MAXN]; 2 int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt; 3 stack<int> S; 4 void Tarjan(int u) 5 { 6 pre[u] = lowlink[u] = ++dfs_clock; 7 S.push(u); 8 for (int i = 0; i < G[u].size(); ++i) { 9 int v = G[u][i]; 10 if (!pre[v]) { 11 Tarjan(v); 12 lowlink[u] = min(lowlink[u], lowlink[v]); 13 } 14 else if (!sccno[v]) { 15 lowlink[u] = min(lowlink[u], pre[v]); 16 } 17 } 18 if (lowlink[u] == pre[u]) { 19 scc_cnt++; 20 int x; 21 do { 22 x = S.top(); 23 S.pop(); 24 sccno[x] = scc_cnt; 25 if (x == u) break; 26 } while (x != u); 27 } 28 } 29 30 void find_scc(int n) 31 { 32 dfs_clock = scc_cnt = 0; 33 memset(sccno,0,sizeof(sccno)); 34 memset(pre,0,sizeof(pre)); 35 for (int i = 0; i < n; ++i) 36 if (!pre[i]) Tarjan(i); 37 }
标签:
原文地址:http://www.cnblogs.com/usedrosee/p/4322168.html