双连通分量

双连通分量就是无向图中的强连通分量,基本就是找割顶和桥。割顶就是一个点,如果把它取掉,连通分量数量就会增加,桥就是一条边,同理。

对于一个连通图,如果任意两点至少存在两条“点不重复”的路径,也就是任意两条边都在一个简单环中,即内部无割顶,则说这个图是点双连通的。

对于一个连通图,如果任意两点至少存在两条“边不重复”的路径,也就是任意边都至少在一个简单环中,即所有边都不是桥,则说这个图是边双连通的。

具体的看白书吧:

点—双连通分量:

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 10000;
int pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;//bccno是判断当前点是否已经入块
typedef pair<int, int> Pair;
vector<int> g[maxn], bcc[maxn];//bcc是存放每个块中有的点
stack<Pair> s;//栈存放当前块中的边

int dfs(int u, int fa)
{
	int lowu = pre[u] = ++dfs_clock;
	int child = 0;
	for (int i = 0; i < g[u].size(); i++)
	{
		int v = g[u][i];
		Pair e = make_pair(u, v);
		if (!pre[v])
		{
			s.push(e);
			child++;
			int lowv = dfs(v, u);
			lowu = min(lowu, lowv);
			if (lowv >= pre[u])
			{
				iscut[u] = 1;
				bcc_cnt++;
				bcc[bcc_cnt].clear();
				while (1)//将当前栈中的边存储
				{
					Pair x = s.top(); s.pop();
					if (bccno[x.first] != bcc_cnt)
					{
					bcc[bcc_cnt].push_back(x.first);
					bccno[x.first] = bcc_cnt;
					}
					if (bccno[x.second] != bcc_cnt)
					{
					bcc[bcc_cnt].push_back(x.second);
					bccno[x.second] = bcc_cnt;
					}
					if (x.first == u&&x.second == v)
					break;
				}

			}
		}
		else if (pre[v] < pre[u] && v != fa)//已经访问过就更新low
		{
			s.push(e);
			lowu = min(lowu, pre[v]);
		}
		
	}
	if (fa < 0 && child == 1)//只有一个子节点的根节点不是割顶
		iscut[u] = 0;
	return lowu;
}
void find_bcc(int n)
{
	memset(pre, 0, sizeof(pre));
	memset(iscut, 0, sizeof(iscut));
	memset(bccno, 0, sizeof(bccno));
	dfs_clock = bcc_cnt = 0;
	for (int i = 1; i <= n; i++)
		if (!pre[i])
			dfs(i, -1);
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	find_bcc(n);
	printf("There are %d cnt in the graph,they are: ", bcc_cnt);
	for (int i = 1; i <= n; i++)
		if (iscut[i])
			printf("%d ", i);
	printf("\n");
	for (int i = 1; i <= bcc_cnt; i++)
	{
		printf("The points in the block %d: ",i);
		vector<int>::iterator it;
		for (it = bcc[i].begin(); it != bcc[i].end(); it++)
			printf("%d ", *it);
		printf("\n");
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值