当前位置:网站首页>1183. 电力
1183. 电力
2022-06-23 03:50:00 【追寻远方的人】
给定一个由 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
思路:
/* 不同的点双连通分量最多只有一个公共点 即某一个割点 任意一个割点都是至少两个点双连通分量的公共点 1 统计连通块个数cnt 2 枚举从哪个块j中删 2.1 从块j中删除哪个点i 2.2 删除点i后块j分成s部分(在样例3中删2后s=0) 总共分成的部分 = s(i新的子块的个数)+cnt(删前总的连通块数)-1(删前子块的个数) = 当前块的部分(s)+剩余连通块的数量(cnt-1) dfn(x)<=low(y) x删掉后y单独出来--多一个单独子树 如果x非根节点 还要多+1 / x / \ 问题转化为依次删除每个割点 求全局最大值 */
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, M = 30010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int ans;
// 没有要求把连通分量每个点找出来 不需要栈
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
void tarjan(int u, int root)
{
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, root);
low[u] = min(low[u], low[j]);
if (low[j] >= dfn[u]) // 判断连通块 有可能是割点(若不是根节点) 去掉该点后的连通块个数>=割点数
cnt++;
}
else // 不需要其他边的要求
low[u] = min(low[u], dfn[j]);
}
if (u != root) // 如果不是根节点 上面还有一个分块
cnt++;
ans = max(ans, cnt);
}
int main()
{
while (cin >> n >> m, n || m)
{
memset(h, -1, sizeof h);
memset(dfn, 0, sizeof dfn);
memset(low, 0, sizeof low);
idx = timestamp = ans = 0;
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
int cnt = 0;
for (int i = 0; i < n; i++)
{
if (!dfn[i])
{
tarjan(i, i);
cnt++;
}
}
printf("%d\n", ans + cnt - 1);
}
return 0;
}
边栏推荐
- PTA:6-73 函数调用
- Qt 及QT VS Tools插件官方下载及安装
- leetcode 91. Decode Ways 解码方法(中等)
- 2022年起重机械安全管理考试题库及答案
- It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
- OpenJudge NOI 1.13 50:数根
- Cool mouse following animation JS plug-ins 5
- Tables de recherche statiques et tables de recherche statiques
- X24cxx series EEPROM chip C language universal reading and writing program
- Pytorch---Pytorch进行自定义Dataset
猜你喜欢

#17生成器的函数声明与调用

Avltree - arbre de recherche binaire équilibré

【一起上水硕系列】Day Three - preview4
![[pytoch] calculate the derivative of sin (x) by automatic differentiation](/img/a7/16dd9ecc13a986a9141ecc3fba00a1.png)
[pytoch] calculate the derivative of sin (x) by automatic differentiation
![[multimode] unimo](/img/a5/a857e20e1432ef3623527c8655a49a.png)
[multimode] unimo

X24cxx series EEPROM chip C language universal reading and writing program

What are the characteristics of SRM supplier management system developed by manufacturing enterprises

If you want to understand PostgreSQL, you must first brush the architecture

Online JSON to CSharp (c) class tool

Implementation of VGA protocol based on FPGA
随机推荐
PTA:7-37 学号解析
PTA:7-69 数据的间距问题
Pta:7-60 pet growth
After the exception is thrown, the @transactional does not take effect
Sessions and Daemons
Software development in 2022: five realities CIOs should know
论文阅读_关系抽取_CASREL
JVM调优简要思想及简单案例-为什么需要JVM调优?
Redis启动有问题
Online JSON to CSharp (c) class tool
Leetcode 1208. 尽可能使字符串相等
Pytoch --- use pytoch's pre training model to realize four weather classification problems
PTA:7-63 计算高考状元
【二叉树进阶】AVLTree - 平衡二叉搜索树
PTA:7-60 宠物的生长
Avltree - arbre de recherche binaire équilibré
摆烂LuoGu刷题记
Dynamics 365 插件中权限操作
一篇文章学会er图绘制
Sequence table lookup