1183. 电力(tarjan算法判断割点,点的双连通分量)

文章介绍了一个关于无向图的题目,要求在删除一个点后计算连通块的最大数目,涉及到连通性分析和双连通分量算法。

1183. 电力 - AcWing题库

给定一个由 n 个点 m 条边构成的无向图,请你求出该图删除一个点之后,连通块最多有多少。

输入格式

输入包含多组数据。

每组数据第一行包含两个整数 n,m

接下来 m 行,每行包含两个整数 a,b,表示 a,b 两点之间有边连接。

数据保证无重边。

点的编号从 0 到 n−1。

读入以一行 0 0 结束。

输出格式

每组数据输出一个结果,占一行,表示连通块的最大数量。

数据范围

1≤n≤10000
0≤m≤15000
0≤a,b<n

输入样例:
3 3
0 1
0 2
2 1
4 2
0 1
2 3
3 1
1 0
0 0
输出样例:
1
2
2

解析: 

点的双连通分量算法:
                dfn[n]

                low[n]

        1)如何判断割点

                low[y]>=dfn[x]

                情况1:如果 x 不是根节点,x 是割点

                情况2:x 是根节点,至少有两个子节点满足 low[yi]>=dfn[x]

——————————————————————————————————————————

1.求统计连通块的个数

2.枚举从哪个块中删,再枚举删除哪割点个点,再求可以成多少个部分

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
const int N = 10010, M = 30010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int root, ans;

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void tarjan(int u) {
	dfn[u] = low[u] = ++timestamp;
	int cnt = 0;
	for (int i = h[u];~i; i = ne[i]) {
		int j = e[i];
		if (!dfn[j]) {
			tarjan(j);
			low[u] = min(low[u], low[j]);
			if (dfn[u] <= low[j])cnt++;
		}
		else low[u] = min(low[u], dfn[j]);//这里不判断 j 是否是祖先节点是因为即使 low[u] 更新了也无所谓,不影响结果,可以仔细想想
	}
	if (u != root &&cnt)cnt++;
	ans = max(cnt,ans);
}

int main() {
	while (scanf("%d%d", &n, &m)!=EOF) {
		if (n == 0 && m == 0)break;
		memset(dfn, 0, sizeof dfn);  
		memset(h, -1, sizeof h);
		idx = timestamp = 0;
		for (int i = 1,a,b; i <= m; i++) {
			scanf("%d%d", &a, &b);
			add(a, b), add(b, a);
		}
		
		int cnt = 0;
		ans = 0;
		for (root = 0; root < n; root++) {
			if (!dfn[root]) {
				cnt++;
				tarjan(root);
			}
		}
		printf("%d\n", ans + cnt - 1);
	}
	return 0;
}

———————————————————————————————————————2024.2.4

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e4 + 5, M = 3e4 + 5, INF = 0x3f3f3f3f;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int ans, root;

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void tarjan(int u,int from) {
	dfn[u] = low[u] = ++timestamp;
	int cnt = 0;
	for (int i = h[u]; i != -1; i = ne[i]) {
		int j = e[i];
		if (!dfn[j]) {
			tarjan(j, i);
			low[u] = min(low[u], low[j]);
			if (dfn[u] <= low[j])cnt++;
		}
		else if (i != (from ^ 1))low[u] = min(low[u], dfn[j]);
	}
	if (u != root && cnt)cnt++;
	ans = max(ans, cnt);
}

int main() {
	while (cin >> n >> m, n || m) {
		memset(h, -1, sizeof h);
		memset(dfn, 0, sizeof dfn);
		idx = timestamp = 0;
		for (int i = 1, a, b; i <= m; i++) {
			scanf("%d%d", &a, &b);
			add(a, b), add(b, a);
		}
		ans = 0;
		int cnt = 0;
		for (root = 0; root < n; root++) {
			if (!dfn[root]) {
				tarjan(root,-1);
				cnt++;
			}
		}
		printf("%d\n", ans + cnt - 1);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值