tarjan求scc

vector<int> e[N];
int dfn[N],low[N],tot;
int stk[N],instk[N],top;
int scc[N],siz[N],cnt;
void tarjan(int x){
    dfn[x]=low[x]=++tot;
    stk[++top]=x,instk[x]=1;
    for(int y:e[x]){
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(instk[y]){
            low[x]=min(low[x],dfn[y]);
        }
    }
    if(dfn[x]==low[x]){
        int y;++cnt;
        do{
            y=stk[top--];instk[y]=0;
            scc[y]=cnt;
            ++siz[cnt];
        }while(y!=x);
    }
}
posted @ 2025-09-10 21:04  xdhking  阅读(7)  评论(0)    收藏  举报