给定一个由 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;
}
文章介绍了一个关于无向图的题目,要求在删除一个点后计算连通块的最大数目,涉及到连通性分析和双连通分量算法。
2万+

被折叠的 条评论
为什么被折叠?



