详见:http://blog.csdn.net/zakheav/article/details/46424251
const int N = 10005;
2 const int M = 100005;
3
4 struct Edge {
5 int to, next;
6 } edge[M];
7 int head[N];
8 int cnt_edge;
9
10 void add_edge(int u, int v)
11 {
12 edge[cnt_edge].to = v;
13 edge[cnt_edge].next = head[u];
14 head[u] = cnt_edge;
15 cnt_edge++;
16 }
17
18 int dfn[N];
19 int low[N];
20 int stk[N];
21 bool in[N];
22 int kind[N];
23
24 int top;
25 int idx;
26 int cnt;
27
28 int n, m;
29
30 void dfs(int u)
31 {
32 dfn[u] = low[u] = ++idx;
33 in[u] = true;
34 stk[++top] = u;
35 for (int i = head[u]; i != -1; i = edge[i].next)
36 {
37 int v = edge[i].to;
38 if (!dfn[v])
39 {
40 dfs(v);
41 low[u] = min(low[v], low[u]);
42 }
43 else if(in[v])
44 {
45 low[u] = min(low[u], dfn[v]);
46 }
47 }
48
49 if (low[u] == dfn[u])
50 {
51 ++cnt;
52 int j;
53 do {
54 j = stk[top--];
55 in[j] = false;
56 kind[j] = cnt;
57 } while (j != u);
58 }
59 }
60
61 void init()
62 {
63 memset(dfn, 0, sizeof dfn);
64 memset(head, -1, sizeof head);
65 cnt_edge = 0;
66 top = cnt = idx = 0;
67 }