tarjan有向图缩点

void tarjan(int x){
    low[x]=dfn[x]=++num;
    s[++top]=x;
    v[x]=1;
    for(int i=head[x];i;i=nex[i]){
        int y=e[i];
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(v[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]){
        cnt++;
        do{
            int y=s[top];
            p[y]=cnt;
            siz[cnt]++;
            v[y]=0;
        }while(x!=s[top--]);
    }
}
发布了51 篇原创文章 · 获赞 7 · 访问量 1940

猜你喜欢

转载自blog.csdn.net/Fooooooo/article/details/104908140