连通分量

开坑,立flag

http://blog.csdn.net/lin375691011/article/details/18774187
https://www.byvoid.com/zhs/blog/scc-tarjan
连通分量好难啊啊啊啊啊

对于有向图中,连通分量叫强连通分量
对于无向图中,连通分量叫双连通分量,而在双连通分量中,又分为点双连通和边双连通。
重点讨论双连通的情况:
以割点区分连通情况的双连通叫做点双连通分量,以割边区分连通情况的双连通叫做边双连通分量。

先贴波模板

强连通分量

#include<stdio.h>
#include<stack>
#include<vector>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
vector<int> G[10005];
stack<int> s;
int inst[10005];
int dfn[10005];
int low[10005];
int index=0;
int num[10005];
int flag=0;
int scn[10005];
int scN;
void tarjan(int u){
    index++; 
    dfn[u]=low[u]=index;
    inst[u]=1;
    s.push(u);
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!scn[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u]){
        scN++;
        int v;
        do{
            v=s.top();
            s.pop();
            scn[v]=scN;
        }while(u!=v);
    }
}

点双连通分量

#include<stdio.h>
#include<algorithm>
#include<stack>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
int dfn[1005],low[1005];
vector<int> G[1005],bcc[1005];
int Bcc;
struct edge{
    int u,v;
};
stack<edge> s;
int son;
int index;
int iscut[1005];
int bccno[1005];
void tarjan(int u){
    index++;
    dfn[u]=low[u]=index;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!dfn[v]){
            s.push(edge{u,v});
            tarjan(v);
            low[u]=min(low[v],low[u]);
            if(low[v]>=dfn[u]){
                iscut[u]++;
                Bcc++;
                bcc[Bcc].clear();
            if(u==1){
                son++;
                }
                while(!s.empty()){
                    edge tmp=s.top();
                    s.pop();
                    if(bccno[tmp.u]!=Bcc){
                        bccno[tmp.u]=Bcc;
                        bcc[Bcc].push_back(tmp.u);
                    }
                    if(bccno[tmp.v]!=Bcc){
                        bccno[tmp.v]=Bcc;
                        bcc[Bcc].push_back(tmp.v);
                    }
                    if(u==tmp.u&&v==tmp.v) break;
                }
            }
        }
        else if(dfn[v]<dfn[u]){
            s.push(edge{u,v});
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(son>1){
        iscut[1]=true;
    }
}

边双连通分量

#include<stdio.h>
#include<algorithm>
#include<stack>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
int dfn[1005],low[1005];
vector<int> G[1005],bcc[1005];
int Bcc;
struct edge{
    int u,v;
};
stack<int> s;
int son;
int index;
int iscut[1005];
int bccno[1005];
int times=1;
int cnt=1;
int group[300];
void tarjan(int u,int fa)
{

    dfn[u]=low[u]=times++;
    s.push(u);
    bool flag=true;
    for(int i=0;i<G[u].size();i++)
    {
        if(flag&&G[u][i]==fa)
        {
            flag=false;
            continue;
        }

        if(!dfn[G[u][i]])
        {
            tarjan(G[u][i],u);
            low[u]=min(low[u],low[G[u][i]]);
        }
        else
        {
            low[u]=min(low[u],dfn[G[u][i]]);
        }
    }
    if(low[u]==dfn[u])
    {
        int v;
        do
        {
            v=s.top();
            s.pop();
            group[v]=cnt;
        }while(v!=u);
        cnt++;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数据结构与算法--图论之寻找连通分量、强连通分量 寻找无向图的连通分量 使用深度优先搜索可以很简单地找出一幅图的所...
    sunhaiyu阅读 9,654评论 2 11
  • tarjan算法实现,low数组代表该点最先追溯到的编号,dfn数组代表该点按照访问次序编的号。 强连通分量:有向...
    Joseph_Z阅读 765评论 0 0
  • 双连通分量 点_双连通分量 BCC对于一个连通图,如果任意两点至少存在两条“点不重复”的路径,则说图是点双连通的(...
    Gitfan阅读 2,194评论 0 0
  • 点双连通分量之割点http://www.cnblogs.com/en-heng/p/4002658.html这篇说...
    陌路晨曦阅读 567评论 0 0
  • 计算机系DSA第二次Programming Assignment中第三题涉及到这个算法 【问题描述】 一个有向图中...
    nnznk阅读 1,196评论 0 0