Tarjan 求强连通分量模板

 1 vector<int> G[MAXN];

 2 int dfn[MAXN], low[MAXN], instk[MAXN], sccno[MAXN], ans[MAXN];

 3 stack<int> S;

 4 int Time, cnt ,n, res;

 5 void Tarjan(int u)

 6 {

 7     dfn[u] = low[u] = ++Time;

 8     S.push(u);

 9     instk[u] = 1;

10     for (int i = 0; i < G[u].size(); ++i) {

11         int v = G[u][i];

12         if (!dfn[v]) {

13             Tarjan(v);

14             low[u] = min(low[u], low[v]);

15         }

16         else if (instk[v]) {

17             low[u] = min(low[u], dfn[v]);

18         }

19     }

20     if (low[u] == dfn[u]) {

21         cnt++;

22         int v;

23         do{

24             v = S.top();

25             S.pop();

26             instk[v] = 0;

27             sccno[v] = cnt;

28         } while (v != u);

29     }

30 }

31 

32 void find_scc()

33 {

34     for (int i = 1; i <= n; ++i)

35         if (!dfn[i]) Tarjan(i);

36 }

 

你可能感兴趣的:(tar)