Tarjan求割边 + UVA 796

  • 割点去除后生成>=2的子图
  • dfs遍历的无向图每个点一次所组成的树就是搜索树

割边

从无向图删除边e之后,G分裂成2个不相连的子图.称e是图G或者割边

  • 割去除后生成2个子图

割边判定法则

  1. 当且仅当搜索树存在x的一个子节点y,满足dfn[x] < low[y]

low[y] > dfn[x]说明:y点及其子树的所有点 能访问到的搜索树中最早的点比x点还晚,那么就是不能通过(x,y)这条边,如果删除(x,y)这条边,y点这部分必然是一个独立的子图或者(y一个点也算图)

  1. 由于无向图是2条有向边.在Tarjan()更新low[]与dfn[]关系时,要去掉父节点
但是可能2点之间存在重边,它们中只有一条在搜索树上.这些不在搜索树上的2点间的重边,仍可以用来更新low[]与dfn[]的关系
  • 需要用一个方法既能解决重边,也能标记父亲节点

记录递归进入每个节点的边的编号.编号处理成邻接表的下标

  • 运用成对变换的技巧.即双向边按照下标23. 45存入邻接表
  • 通过异或直接找到反向边:2^1=3 并且 3^1=2.这样就能除去与父节点连接的边而不影响与父节点之间的重边

例题:UVA 796

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
	
const int maxn = 1e5 + 10;
int head[maxn], next1[maxn << 1], ver[maxn << 1];
int dfn[maxn], low[maxn];
int n,tot,num;
bool bridge[maxn << 1];

void edgeadd(int x,int y) {
	ver[++tot] = y; next1[tot] = head[x]; head[x] = tot;
}
void tarjan(int x,int in_edge) {
	dfn[x] = low[x] = ++num;
	for (int i = head[x]; i; i = next1[i]) {
		int y = ver[i];
		if (!dfn[y]) {
			tarjan(y, i);
			low[x] = min(low[x], low[y]);

			if (low[y] > dfn[x]) {
				bridge[i] = bridge[i ^ 1] = 1;
			}
		}
		else if (i != (in_edge ^ 1))
			low[x] = min(dfn[y],low[x]);
	}
}
void init() {
	tot = 1; num = 0;

	memset(low,0,sizeof low);
	memset(dfn,0,sizeof dfn);

	memset(head, 0, sizeof head); 
	memset(next1, 0, sizeof next1);
	memset(ver,0,sizeof ver);

	memset(bridge,0,sizeof bridge);
}
int main() {
//	freopen("a.txt","r",stdin);
	while (~scanf("%d",&n)) {
		init();
		if (n == 0) {
			printf("0 critical links\n\n");
			continue;
		} 
		for (int i = 1; i <= n; i++) {
			int cur, cnt;
			scanf("%d (%d)",&cur,&cnt); 
			cur++;
			for (int j = 1; j <= cnt; j++) {
				int to; scanf("%d", &to); 
				to++;
				if (to <= cur) continue;
				edgeadd(cur, to); 
				edgeadd(to,cur);
			}
		}
		for (int i = 1; i <= n; i++)
			if (!dfn[i]) tarjan(i,0);
		int ans = 0;
		for (int i = 2; i < tot; i+=2) //邻接表存边从2开始
			if (bridge[i]) ans++;
	
		printf("%d critical links\n",ans);
		for (int i = 2; i < tot; i += 2) 
			if (bridge[i]) printf("%d - %d\n",ver[i^1] - 1,ver[i] - 1);
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值