时间复杂度:O(n+m).
void tarjan(int u)
{
dfn[u] = low[u] = ++ ixp ;
stp[++tot] = u;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i])
{
int y = e[i];
if(!dfn[y])
{
tarjan(y);
low[u] = min(low[y],low[u]);
}
else if(st[y]) low[u] = min(low[u],dfn[y]);
}
if(dfn[u] == low[u])
{
scv++;
int y;
do
{
y = stp[tot--];
st[y] = false;
id[y] = scv;
}while(y != u);
}
}
int main()
{
for(int i = 1; i <= n; i ++ )
if(!dfn[i])
tarjan(i);
}