Tarjan算法求BCC(无向图连通块、割边、割点)

http://www.byvoid.com/zhs/blog/biconnect

http://blog.csdn.net/shiqi_614/article/details/7833628


#include 
#include 
#include 
#include 
using namespace std;

const int N = 2e3+5;
const int M = 1e4+5;

struct Vertex
{
    int head;
}V[N];

struct Edge
{
    int v,next;
}E[M];

int top,bcc,id,dfn[N],low[N],belong[N],sub[N];

bool in[N],cut_p[N],cut_e[M];

stack S;

void init()
{
    top = 0;
    memset(V,-1,sizeof(V));
}

void add_edge(int u,int v)
{
    E[top].v = v;
    E[top].next = V[u].head;
    V[u].head = top++;
}

void tarjan(int pre,int u)
{
    dfn[u] = low[u] = ++id;
    int cnt = 0;
    S.push(u);
    for(int i=V[u].head;~i;i=E[i].next)
    {
        int v = E[i].v;
        if(v == pre)
            continue;
        if(!dfn[v])
        {
            ++cnt;
            tarjan(u,v);
            low[u] = min(low[u],low[v]);
            if(u!=pre && dfn[u]<=low[v])
            {
                cut_p[u] = true;
                sub[u]++;
            }
            if(dfn[u] < low[v])
                cut_e[i] = true;
        }
        else
            low[u] = min(low[u],dfn[v]);
    }
    if(dfn[u] == low[u])
    {
        int v;
        do
        {
            v = S.top();
            S.pop();
            belong[v] = bcc;
        }while(u != v);
        bcc++;
    }
    if(u==pre && cnt>1)
    {
        cut_p[u] = true;
        sub[u] = cnt-1;
    }
}

void get_bcc_cutp_cute(int n)
{
    bcc = id = 0;
    memset(cut_p,false,sizeof(cut_p));
    memset(cut_e,false,sizeof(cut_e));
    memset(dfn,0,sizeof(dfn));
    memset(sub,0,sizeof(sub));
    for(int i=0;i


你可能感兴趣的:(◆学习笔记,【图论】)