1183. 电力

题目链接

1183. 电力

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

输入格式

输入包含多组数据。

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

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

数据保证无重边。

点的编号从 0n1

读入以一行 0 0 结束。

输出格式

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

数据范围

1n10000,
0m15000,
0a,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

解题思路

缩点,点双连通分量

无向图的点双连通分量的重要概念:割点。即如果删除某点后,整个图的连通性会发生改变,则该点称为割点。不含割点的连通块即点双连通分量。对于一个点 x 来说,设其连向的另外一个点 yx 为割点当且仅当 dfn[x]low[y]

显然,删除的节点即割点,不妨枚举整个无向图的所有连通块的割点,取分成的最多连通块的割点即可

  • 时间复杂度:O(n+m)

代码

// Problem: 电力 // Contest: AcWing // URL: https://www.acwing.com/problem/content/description/1185/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=10005,M=30005; int n,m; int h[N],ne[M],e[M],idx; int timestamp,dfn[N],low[N]; int res,root; void add(int a,int b) { e[idx]=b,ne[idx]=h[a],h[a]=idx++; } void tarjan(int x) { dfn[x]=low[x]=++timestamp; int cnt=0; for(int i=h[x];~i;i=ne[i]) { int j=e[i]; if(!dfn[j]) { tarjan(j); low[x]=min(low[x],low[j]); if(dfn[x]<=low[j])cnt++; } else low[x]=min(low[x],dfn[j]); } if(x!=root)cnt++; res=max(res,cnt); } int main() { while(scanf("%d%d",&n,&m),n||m) { memset(h,-1,sizeof h); memset(dfn,0,sizeof dfn); timestamp=idx=0; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y),add(y,x); } res=0; int cnt=0; for(root=0;root<n;root++) if(!dfn[root]) { cnt++; tarjan(root); } printf("%d\n",res+cnt-1); } return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16907317.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-11-19 树的直径
点击右上角即可分享
微信分享提示