当前位置:网站首页>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;
}
边栏推荐
- flutter系列之:flutter中的Wrap
- PTA:7-63 计算高考状元
- PTA:6-30 时间相加
- Avltree - arbre de recherche binaire équilibré
- OpenJudge NOI 1.13 51:古代密码
- PTA:7-87 集合的模拟实现(类模板)
- PTA:7-58 图书音像出租管理
- 大一学生课设c——服装管理系统
- How to make the page number start from the specified page in word
- Pta:6-29 application of virtual base classes - people, teachers and students
猜你喜欢

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

语料库数据处理个案实例(分词和分句、词频统计、排序)

How to make the page number start from the specified page in word

【多模态】UNIMO

Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测

Svg+js smart home monitoring grid layout

Halcon知识:binocular_disparity 知识

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

Online text filter less than specified length tool

Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
随机推荐
【二叉树进阶】AVLTree - 平衡二叉搜索树
Pytoch --- pytoch customizes the dataset
【多模态】UNIMO
离线数仓建模中常见的概念-术语
Pytoch --- use pytoch's pre training model to realize four weather classification problems
【二叉树】翻转等价二叉树
How to use shell script to monitor file changes
Idea import module
Cocos学习日记1——节点
Imitation 360 desktop suspended ball plug-in
Implementation of VGA protocol based on FPGA
Does the network disk also roll inside?
Getting started with tensorflow
PTA:7-69 数据的间距问题
X24cxx series EEPROM chip C language universal reading and writing program
Ideal car × Oceanbase: when new forces of car building meet new forces of database
Cocos学习日记2——脚本和属性
P1363 phantom maze (DFS)
C语言刷题随记 —— 自由落体的球
Introduction to deep learning