Tarjan (圆方树)

· · 个人记录

强连通分量

void tarjan(int x)
{
    dfn[x]=low[x]=++tim;
    st[++top]=x;inv[x]=1;
    for(auto y : g[x])
    {
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);  
        }   
        else if(inv[y])
        {
            low[x]=min(low[x],dfn[y]);  
        } 
    } 
    if(dfn[x]==low[x])
    {
        if(x!=st[top]) ans++;
        cnt++;int y;
        do
        {

            y=st[top--];
            fa[y]=cnt;
            inv[y]=0;
        }while(x!=y);
    }
}

割点

void tarjan(int x,bool root)
{
    int tot=0;
    dfn[x]=low[x]=++tim;
    for(auto y : g[x])
    {
        if(!dfn[y])
        {
            tarjan(y,0);
            low[x]=min(low[x],low[y]);  
            tot+=low[y]>=dfn[x]; 
        }   
        else low[x]=min(low[x],dfn[y]);
    } 
    if(tot>root) d[++top]=x;
}   

割边

void tarjan(int x)
{
    dfn[x]=low[x]=++tim;
    for(auto y : g[x])
    {
        if(!dfn[y])
        {
            fa[y]=x;
            tarjan(y);
            low[x]=min(low[x],low[y]);  
            tot+=low[y]>dfn[x]; 
        }   
        else if(fa[x]!=y) low[x]=min(low[x],dfn[y]);
    }   
}

点双 边双

void tarjan(int u,int f)
{
    dfn[u]=low[u]=++tim;st[++top]=u;siz++;
    for(auto v:G[u])
    {
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]==dfn[u])
            {
                ++root,val[root]=1; 
                for(int x=0;x^v;--top)
                {
                    x=st[top];
                    val[root]++;
                    g[root].push_back(x);
                    g[x].push_back(root);
                }
                g[root].push_back(u),g[u].push_back(root);
            }
        }
        else 
        {
            low[u]=min(low[u],dfn[v]);
        }
    }   
}