浅谈e-DCC缩点

        e-DCC就是边连通分量,指的是原图中一个极大的连通子图(该子图没有桥)
        e-DCC缩点,就是将同一个边连通分量内的点看作一个点,将桥当作边连通分量之间的边。
        e-DCC缩点首先需要求解所有的边连通分量。可以通过tarjan算法求解边连通分量。用成对变换的技巧处理双向边和重边问题,在tarjan中有两个值,dfn和low,dfn表示的是对图进行深度优先遍历时,首次遍历的时间戳。在通过对图进行深度优先遍历的时候会形成一颗搜索树,low表示的就是通过一条不在搜索树上的边能够回到的时间戳的最小值关于图的连通性问题,往往需要通过搜索树考虑
        缩点的求解:

  1. 求桥
    tarjan求桥,给dfn和low的值,递归更新low的值,如果在递归回溯后 dfn[x] < low[y](x是y的父节点),说明这个边就是一个桥了(子节点在不通过搜索树上的边的情况下无法返回到祖先节点)
  2. 通过对图的dfs求连通块确定边连通分量
    定义一个数组 dcc[x] 表示 x 所属的边连通分量,初始为0或-1(其实什么都行,0和-1是我的习惯,就是做一个区分)在dfs求连通块的时候,遍历所有的点,如果当前的点 dcc 值为初始值,就从这个点开始求连通块,遍历它的子节点,递归求解,遇到某一条边是 或者点的 dcc值已经被求解 就不用求解了
  3. 重新建图(缩点)
    遍历所有的边,如果这个边割边,将边的两个点所在的边连通分量连边。或者也可以遍历边的时候,通过找出边的两个端点,如果两个点不在同一个边连通分量内,就将两个边连通分量连边。
/**
 * Author : correct
 * e-DCC
 */
#include <bits/stdc++.h>
using namespace std;
const int N = 300100;
int head[N], nex[N * 4], to[N * 4], cnt;
int head1[N];
#define debug cout << "-----------------------------------------\n"
#define mem(a, b) memset(a, b, sizeof a)
int n, m;
int dfn[N], low[N], ti;
bool cut[N];
int e_dcc[N], num;
void add(int* h, int a, int b){
    
    
	++cnt;
	to[cnt] = b;
	nex[cnt] = h[a];
	h[a] = cnt;
}
void tarjan(int x, int ind){
    
    //  no multiple edges
	//tarjan 求割边
	dfn[x] = low[x] = ++ti;
	for (int i = head[x]; i; i = nex[i]){
    
    
		int y = to[i];
		if (!dfn[y]){
    
    
			tarjan(y, i);
			low[x] = min(low[x], low[y]);
			if (dfn[x] < low[y]){
    
    
				cut[i] = cut[i ^ 1] = 1;
			}
		}
		else if (ind != (i ^ 1))low[x] = min(low[x], dfn[y]);
	}
}
void dfs(int x){
    
    
	// 求边双连通分量
	// 方法:对原图求连通块,遇到割边continue
	e_dcc[x] = num;
	for (int i = head[x]; i; i = nex[i]){
    
    
		int y = to[i];
		if (e_dcc[y] || cut[i])continue;
		dfs(y);
	}
}
// 缩点建立新图:将每一个边双连通分量看作一个点,将割边看作双连通分量之间的边即可
void rebuild_map(){
    
    
	for (int i = 2; i <= cnt; i++){
    
    
		int x = to[i];
		int y = to[i ^ 1];
		if (e_dcc[x] == e_dcc[y])continue;
		add(head1, e_dcc[x], e_dcc[y]);
	}
}
int main()
{
    
    
#ifdef debug
	ios::sync_with_stdio(0);
	cnt = 1;
#endif
	cin >> n >> m;
	num = n + 1;
	for (int i = 0; i < m; i++){
    
    
		int a, b;
		cin >> a >> b;
		add(head, a, b), add(head, b, a);
	}
	for (int i = 1; i <= n; i++){
    
    
		if (!dfn[i]){
    
    
			tarjan(i, 0);
		}
	}
	for (int i = 1; i <= n; i++){
    
    
		if (!e_dcc[i]){
    
    
			++num;
			dfs(i);
		}
	}
	rebuild_map();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43701790/article/details/105267555
今日推荐