B3610 [图论与代数结构 801] 无向图的块

本文介绍了无向图的点双联通分量(v-DCC),即无向图的极大点双连通子图的概念。通过详细解释算法过程,包括当dfn[x] <= low[y]时如何找到v-DCC,帮助读者理解这一图论中的关键概念。
摘要由CSDN通过智能技术生成

原题链接

点双联通分量(v-DCC, 无向图的极大点双连通子图):

①当第一个节点第一次被访问时,把该节点入栈.
②当dfn[x] <= low[y]成立时(1)从栈顶不断弹出节点,直至节点y被弹出(2)刚才弹出的所有节点与节点x一起构成一个v-DCC.

#include <bits/stdc++.h>

using namespace std; 

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f = ch == '-', ch = getchar(); 
	while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); 
	return f ? -x : x; 
}

void print(int x) {
	if (x < 0) x = -x, putchar('-'); 
	if (x < 10) putchar(x + '0'); 
	else {
		print(x / 10); 
		putchar(x % 10 + '0'); 
	}
}

const int N = 5e4 + 5, M = 3e5 + 5; 
int n, m, rt, head[N], nex[M << 1], ver[M << 1], tot;
int dfn[N], low[N], cut[N], num; 
int stk[N], top; 
vector<int> dcc[N]; int cnt; 

void Addedge(int x, int y) { ver[++tot] = y; nex[tot] = head[x]; head[x] = tot; }

void tarjan(int x) {
	dfn[x] = low[x] = ++num; 
	stk[++top] = x; 
	if (x == rt && !head[x]) {
//		dcc[++cnt].push_back(x); 
		return; 
	}
	int flag = 0; 
	for (int i = head[x]; i; i = nex[i]) {
		int y = ver[i];  
		if (!dfn[y]) {
			tarjan(y); 
			low[x] = min(low[x], low[y]); 
			if (low[y] >= dfn[x]) {
				++flag; 
				if (x != rt || flag > 1) cut[x] = true; 
				++cnt; int z; 
				do {
					z = stk[top--]; 
					dcc[cnt].push_back(z); 
				} while (z != y); 
				dcc[cnt].push_back(x);
			}
		} else low[x] = min(low[x], dfn[y]);
	}
}

bool cmp(vector<int> a, vector<int> b) {
	int mi = min(a.size(), b.size()); 
	for (int i = 0; i < mi; ++i) {
		if (a[i] != b[i]) return a[i] < b[i]; 
	}
	return a.size() < b.size(); 
}

int main() {
	n = read(); m = read(); 
	while (m--) {
		int uu = read(), vv = read(); 
		Addedge(uu, vv); Addedge(vv, uu); 
	}
	for (int i = 1; i <= n; ++i) {
		if (!dfn[i]) {
			rt = i; tarjan(i); 
		}
	}
	print(cnt); putchar('\n'); 
	for (int i = 1; i <= cnt; ++i) sort(dcc[i].begin(), dcc[i].end());
	sort(dcc + 1, dcc + cnt + 1, cmp); 
	for (int i = 1; i <= cnt; ++i) {
		for (vector<int>::iterator it = dcc[i].begin(); it != dcc[i].end(); ++it) {
			print(*it); putchar(' '); 
		}
		putchar('\n'); 
	}
	return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值