连通性详解「割边」「割点」「点双连通分量」「边双连通分量」

定义

  • 割点:对于一个点x,如果从图中删去x以及与x相连的所有的边,图不再联通,则称x为割点

  • 割边:对于一条边e,从图中删去e,图不联通,则称e为割边

  • 一个图如果不存在割点,则它是一个点双连通图,一个图的极大点双连通子图是他的点双连通分量

  • 一个图如果不存在割边,则它是一个边双连通图,一个图的极大边双连通子图是他的边双连通分量

Tarjan

  • 时间戳dfn:第几个搜到这个点

  • 返祖边:搜索树上一个点连向其祖先节点的边

  • 横插边:搜索树上一个点连向它另一条支链上的点的边(只存在于有向图

  • 追溯值low:经过一次返祖边能到达的最上面的点(可以先往下走到有返祖边的地方再走返祖边

连通性详解「割边」「割点」「点双连通分量」「边双连通分量」_第1张图片

连通性详解「割边」「割点」「点双连通分量」「边双连通分量」_第2张图片

左边是一个无向图,右边是经过dfs后形成的树,绿色的是反祖边,紫色的是时间戳dfn,蓝色的是追溯值low

u是割点的条件是:

  • v是u搜索树上的一个儿子:dfn[u] <= low[v]——v的子树中没有返祖边能跨越u点
  • 有多个儿子的根结点

比如上述的十号节点

< u-v >是桥的条件是:

  • 搜索树上v树u的儿子:dfn[u] < low[v]——v的子树中没有返祖边能跨越< u-v >这条边

比如上述的<8-10>边

题单

  • 可达性 有向图 边双缩点模版题
  • [HAOI2006]受欢迎的牛 有向图 边双缩点
  • 嗅探器 无向图 割点
  • Network of Schools 有向图,缩点,问形成强连通要几条边
  • Cow Ski Area 不建图,滑雪题
  • [HAOI2006]受欢迎的牛 有向图、缩点
  • [ZJOI2007]最大半连通子图 有向图、缩点、拓扑排序、dp
  • Redundant Paths 无向图、桥、边双连通分量缩点

求割点的板子题

P3388 【模板】割点(割顶)

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

//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define eps 1e-8
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f
#define NMAX 200 + 50
#define MAX 200000 + 50
#define mod 998244353
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
#define m_p(a,b) make_pair(a, b)
//#define max(a,b) (((a)>(b)) ? (a):(b))
//#define min(a,b) (((a)>(b)) ? (b):(a))

typedef  long long ll;
typedef pair <int,int> pii;
typedef unsigned long long ull;
//不开longlong见祖宗!不看范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}

int n, m;
int a, b;

int tot;
int head[MAX];
struct ran{
    int to, nex;
}tr[MAX];
void add(int u, int v){
    tr[++tot].to = v;
    tr[tot].nex = head[u];
    head[u] = tot;
}

bool g[MAX];

int cnt;
int dfn[MAX], low[MAX];
void tarjan(int u, int root){
    int flag = 0;//记录子节点数量
    dfn[u] = low[u] = ++cnt;//更新时间戳
    for(int i = head[u]; i; i = tr[i].nex){
        int v = tr[i].to;
        if(!dfn[v]){
            tarjan(v, root);
            low[u] = min(low[u], low[v]);//回溯更新low值
            if(low[v] >= dfn[u]){
                ++flag;
                if(u != root || flag > 1)g[u] = 1;//如果不是根节点或者是跟节点且子子点数量大于1就说明u是割点
            }
        }
        else low[u] = min(low[u], dfn[v]);//返祖边
    }
}

void work(){
    sdd(n, m);
    for(int i = 1; i <= m; ++i){
        sdd(a, b);
        add(a, b);
        add(b, a);
    }
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])tarjan(i, i);
    }
    int num = 0;
    for(int i = 1; i <= n; ++i){
        num += g[i];
    }
    cout<<num<<endl;
    for(int i = 1; i <= n; ++i){
        if(g[i])cout<<i<<' ';
    }
    cout<<endl;
}

int main(){
    work();
    return 0;
}

割边模版题

Critical Links

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

//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define eps 1e-8
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f
#define NMAX 1000 + 50
#define MAX 1000000 + 50
#define mod 998244353
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
#define m_p(a,b) make_pair(a, b)
//#define max(a,b) (((a)>(b)) ? (a):(b))
//#define min(a,b) (((a)>(b)) ? (b):(a))

typedef  long long ll;
typedef pair <int,int> pii;
typedef unsigned long long ull;
//不开longlong见祖宗!不看范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}

int n;
int x, y;
int num;
char p;

int tot;
int head[MAX];
struct ran{
    int to, nex;
    bool vis;
}tr[MAX];
inline void add(int u, int v){
    tr[++tot].to = v;
    tr[tot].nex = head[u];
    head[u] = tot;
}

int tim;
int bridge;
int dfn[NMAX], low[NMAX];
inline void tarjan(int u, int fa){
    dfn[u] = low[u] = ++tim;
    for(int i = head[u]; i; i = tr[i].nex){
        int v = tr[i].to;
        if(v == fa)continue;//如果是到父亲节点就跳过
        if(!dfn[v]){
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] > dfn[u]){
                ++bridge;//桥的数量
                tr[i].vis = tr[i ^ 1].vis = 1;//注意,标记的时候要标俩
            }
        }
        else low[u] = min(low[u], dfn[v]);
    }
}

void work(){
    for(int i = 1; i <= n; ++i){
        cin>>x>>p>>num>>p;
        while (num--) {
            sd(y);
            add(x, y);
            add(y, x);
        }
    }
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])tarjan(i, i);
    }
    vector<pii>v;//因为题目要求从小到大,且u<=v
    for(int i = 0; i <= tot; i += 2){
        if(tr[i].vis){
            if(tr[i].to < tr[i ^ 1].to)v.push_back(m_p(tr[i].to, tr[i ^ 1].to));
            else v.push_back(m_p(tr[i ^ 1].to, tr[i].to));
        }
    }
    sort(v.begin(), v.end());
    cout<<bridge<<" critical links\n";
    for(int i = 0; i < v.size(); ++i){
        cout<<v[i].first<<" - "<<v[i].second<<endl;
    }
    cout<<endl;
}

void init(){//初始化
    mem(head, 0);
    mem(tr, 0);
    mem(dfn, 0);
    mem(low, 0);
    tot = -1;//必须是初始化为-1
    tim = bridge = 0;
}

int main(){
    while (sd(n) != EOF) {
        init();
        work();
    }
}

双连通

点双连通

// Author: Chelsea
// 2021.09.17
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define eps 1e-8
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f
#define NMAX 500000 + 50
#define MAX 200000 + 50
#define mod 998244353
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
#define m_p(a,b) make_pair(a, b)
//#define max(a,b) (((a)>(b)) ? (a):(b))
//#define min(a,b) (((a)>(b)) ? (b):(a))

typedef  long long ll;
typedef pair <int,int> pii;
typedef unsigned long long ull;
//不开longlong见祖宗!不看范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}

int n, m;
int a, b;

int tot;
int head[MAX];
struct ran{
    int to, nex;
}tr[MAX];
void add(int u, int v){
    tr[++tot].to = v;
    tr[tot].nex = head[u];
    head[u] = tot;
}

int dfn[MAX], low[MAX];
int tim;//用于更新每个点的dfn
stack<int>st;
int cnt;//记录点双连通的数量
vector<int>bcc[MAX];//存点双连通图的点
void tarjan(int u, int fa){
    st.push(u);
    dfn[u] = low[u] = ++tim;
    for(int i = head[u]; i; i = tr[i].nex){
        int v = tr[i].to;
        if(!dfn[v]){
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] >= dfn[u]){//u是割点
                ++cnt;//点双数量+1
                bcc[cnt].push_back(u);//先把割点u扔进去
                while (st.top() != v) {//一直弹到v
                    bcc[cnt].push_back(st.top());
                    st.pop();
                }
                bcc[cnt].push_back(st.top());st.pop();//把v弹出
            }
        }
        else if(v != fa)low[u] = min(low[u], dfn[v]);//如果访问过了,就得判断一下是不是返祖边,而非父边
    }
}

void work(){
    sdd(n, m);
    for(int i = 1; i <= m; ++i){
        sdd(a, b);
        add(a, b);
        add(b, a);
    }
    for(int i = 1; i <= n; ++i){
        if(!dfn[i]){
            tarjan(i, i);
        }
    }
    for(int i = 1; i <= cnt; ++i){
        for(int j = 0; j < bcc[i].size(); ++j)cout<<bcc[i][j]<<' ';
        cout<<endl;
    }
    
}

int main(){
//    int _t;sd(_t);
//    while (_t--) {
        work();
//    }
    return 0;
}
/*
 10 13
 1 2
 2 3
 3 5
 3 8
 4 8
 7 8
 8 9
 6 9
 6 10
 2 5
 1 4
 9 10
 8 10
 */

边双连通,缩点

无向图:Redundant Paths

// Author: Chelsea
// 2021.09.17
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define eps 1e-8
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f
#define NMAX 5000 + 50
#define MAX 200000 + 50
#define mod 998244353
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
#define m_p(a,b) make_pair(a, b)
//#define max(a,b) (((a)>(b)) ? (a):(b))
//#define min(a,b) (((a)>(b)) ? (b):(a))

typedef  long long ll;
typedef pair <int,int> pii;
typedef unsigned long long ull;
//不开longlong见祖宗!不看范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}

int n, m;
int a, b;

int tot = 1;
int head[MAX];
struct ran{
    int to, nex;
}tr[MAX];
void add(int u, int v){
    tr[++tot].to = v;
    tr[tot].nex = head[u];
    head[u] = tot;
}

int dfn[MAX], low[MAX];
int tim;
stack<int>st;
int cnt;
bool vis[MAX];
bool g[MAX];
int color[MAX];
void tarjan(int u){
    dfn[u] = low[u] = ++tim;
    st.push(u);
    vis[u] = 1;
    for(int i = head[u]; i; i = tr[i].nex){
        int v = tr[i].to;
        if(g[i])continue;
        g[i] = g[i ^ 1] = 1;
        if(!dfn[v]){
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]){
        ++cnt;
        int now;
        do {
            now = st.top();
            st.pop();
            vis[now] = 0;
            color[now] = cnt;
        } while (now != u);
    }
}

int in[MAX];

void work(){
    sdd(n, m);
    for(int i = 1; i <= m; ++i){
        sdd(a, b);
        add(a, b);
        add(b, a);
    }
    for(int i = 1; i <= n; ++i)if(!dfn[i])tarjan(i);
    for(int u = 1; u <= n; ++u){
        for(int i = head[u]; i; i = tr[i].nex){
            int v = tr[i].to;
            if(color[u] != color[v]){
                ++in[color[v]];
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= cnt; ++i){
        if(in[i] == 1)++ans;
    }
    cout<<(ans + 1) / 2<<endl;
}

int main(){
//    int _t;sd(_t);
//    while (_t--) {
        work();
//    }
    return 0;
}

有向图,缩点

可达性

// Author: Chelsea
// 2021.09.18
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define eps 1e-8
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f
#define NMAX 5000 + 50
#define MAX 200000 + 50
#define mod 998244353
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
#define m_p(a,b) make_pair(a, b)
//#define max(a,b) (((a)>(b)) ? (a):(b))
//#define min(a,b) (((a)>(b)) ? (b):(a))

typedef  long long ll;
typedef pair <int,int> pii;
typedef unsigned long long ull;
//不开longlong见祖宗!不看范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}

int n, m;
int a, b;

int tot = 0;
int head[MAX];
struct ran{
    int to, nex;
}tr[MAX];
void add(int u, int v){
    tr[++tot].to = v;
    tr[tot].nex = head[u];
    head[u] = tot;
}

int tim;
int dfn[MAX], low[MAX];
bool vis[MAX];
stack<int>st;
int color[MAX];
int cnt;
void tarjan(int u){
    st.push(u);
    vis[u] = 1;
    dfn[u] = low[u] = ++tim;
    for(int i = head[u]; i; i = tr[i].nex){
        int v = tr[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]){
        ++cnt;
        int now;
        do {
            now = st.top();
            color[now] = cnt;
            st.pop();
            vis[now] = 0;
        } while (now != u);
    }
}

bool p[MAX];
int in[MAX];

void work(){
    sdd(n, m);
    for(int i = 1; i <= m; ++i){
        sdd(a, b);
        add(a, b);
    }
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])tarjan(i);
    }
    for(int u = 1; u <= n; ++u){
        for(int j = head[u]; j; j = tr[j].nex){
            int v = tr[j].to;
            if(color[u] != color[v]){
                ++in[color[v]];
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= cnt; ++i){
        if(!in[i])++ans;
    }
    cout<<ans<<endl;
    for(int i = 1; i <= n; ++i){
        if(!in[color[i]] && !p[color[i]]){
            cout<<i<<' ';
            p[color[i]] = 1;
        }
    }
    cout<<endl;
}

int main(){
    work();
    return 0;
}

你可能感兴趣的:(图论,算法,数据结构,连通性)