边双连通分支模板

int tol,head[maxn];
struct edge
{
    int to,next;
    bool cut;
}es[maxm];
void addedge( int u , int v )
{
    es[tol].to = v;
    es[tol].next = head[u];
    es[tol].cut = false;
    head[u] = tol++;
}
int Low[maxn],Dfn[maxn],Stack[maxn],Belong[maxn];
int Index,Top,Block,Bridge,du[maxn];
bool Instack[maxn];
void dfs( int u , int f )
{
    int v;
    Low[u] = Dfn[u] = ++Index;
    Stack[Top++] = u;
    Instack[u] = true;
    for ( int i=head[u] ; i!=-1 ; i=es[i].next )
    {
        v = es[i].to;
        if ( v==f ) continue;
        if ( !Dfn[v] )
        {
            dfs( v , u );
            if ( Low[u]>Low[v] ) Low[u] = Low[v];
            if ( Low[v]>Dfn[u] )
            {
                Bridge++;
                es[i].cut = true;
                es[i^1].cut = true;
            }
        }
        else if ( Instack[v]&&Low[u]>Dfn[v] )
            Low[u] = Dfn[v];
    }
    if ( Low[u]==Dfn[u] )
    {
        Block++;
        do
        {
            v = Stack[--Top];
            Instack[v] = false;
            Belong[v] = Block;
        }
        while ( v!=u );
    }
}
void Tarjan()
{
    memset ( Dfn , 0 , sizeof(Dfn) );
    memset ( Instack , false , sizeof(Instack) );
    Index = Top = Block = 0;
    dfs( 1 , 1 );
    int ans = 0;
    memset ( du , 0 , sizeof(du) );
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=head[i] ; j!=-1 ; j=es[j].next )
            if ( es[j].cut ) du[Belong[i]]++;
    for ( int i=1 ; i<=Block ; i++ )
        if ( du[i]==1 ) ans++;
    printf ( "%d\n" , (ans+1)/2 );
}

 

你可能感兴趣的:(图论模板)