美文网首页
连通分量

连通分量

作者: 陌路晨曦 | 来源:发表于2017-07-19 10:55 被阅读0次

开坑,立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++;
    }
}

相关文章

  • 活动分组——无向图的连通分量个数

    一、相关概念 连通分量 无向图中,极大连通子图称为连通分量1)连通图的连通分量只有一个,即自身2)非连通的无向图有...

  • 图的连通性

    一、连通分量 1.1 定义 连通分量是针对无向图的,无向图G的极大连通子图称为G的连通分量( Connected ...

  • quick-find

    要实现的API: 初始化 返回连通分量的count 合并还未连通的分量 判断是否连通 思路: 如果还未连通(con...

  • tarjan

    tarjan:寻找出度为0的强连通分量,并求出该强连通分量中有多少个点。 sig表示的是强连通分量的个数其中col...

  • 连通分量

    tarjan算法实现,low数组代表该点最先追溯到的编号,dfn数组代表该点按照访问次序编的号。 强连通分量:有向...

  • 连通分量

    开坑,立flag http://blog.csdn.net/lin375691011/article/detail...

  • 数据结构与算法--图论之寻找连通分量、强连通分量

    数据结构与算法--图论之寻找连通分量、强连通分量 寻找无向图的连通分量 使用深度优先搜索可以很简单地找出一幅图的所...

  • 无向图的双连通分量

    双连通分量 点_双连通分量 BCC对于一个连通图,如果任意两点至少存在两条“点不重复”的路径,则说图是点双连通的(...

  • tarjan

    tarjan寻找出度为0的强连通分量,从小到大输出此强连通分量中的点 poj 2553 The Bottom of...

  • tarjan-寻找图中有多少个强连通分量

    tarjan寻找图中有多少个强连通分量 hdu 1269 迷宫城堡判断图否是属于一个强连通分量

网友评论

      本文标题:连通分量

      本文链接:https://www.haomeiwen.com/subject/wkjxkxtx.html