tarjan

· · 个人记录

void tarjan(int u){
    dfn[u]=low[u]=++tot;
    s.push(u);
    in_stack[u]=1;
    for(auto v:e[u]){
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[v],low[u]);
        }else if(in_stack[v]){
            low[u]=min(dfn[v],low[u]);
        }
    }
    if(dfn[u]==low[u]){
        cnt++;
        while(1){
            int v=s.top();
            scc[v]=cnt;
            in_stack[v]=0;
            s.pop();
            if(v==u)break;
        }
    }
}