无向连通图求割点(tarjan算法去掉改割点剩下的联通分量数目)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 3603 | Accepted: 1213 |
Description
not enough power in one area, while there is a large surplus in the rest of the country.
ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places
- i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun.
One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network
to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points.
Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining
points (not counting the removed joining point itself).
Input
The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines
of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection
between every two plants.
The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros.
Output
one of the joining points at power plants in the instance.
Sample Input
3 3
0 1
0 2
2 1
4 2
0 1
2 3
3 1
1 0
0 0
Sample Output
1
2
2
题目大题:
求去掉某个点以及与其相连的边,最多可以形成多少个连通分量:
tarjan算法求割点
程序:
#include"string.h"
#include"stdio.h"
#include"iostream"
#include"queue"
#include"stack"
#define M 10009
#include"stdlib.h"
#include"math.h"
#define inf 99999999
using namespace std;
struct node
{
int u,v,next;
}edge[M*20];
int t,head[M],low[M],dfn[M],index,cut[M],sum,root,s,num[M];
//cut[]可以记录去掉该节点后导致形成多少个连通分量
//num[]可以记录以改点为根的连通图有多少个元素
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u,int fa)
{
s++;
dfn[u]=low[u]=++index;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
cut[u]++;
}
else
low[u]=min(low[u],dfn[v]);
}
if(fa<0)
cut[u]--;
}
void solve(int n)
{
index=sum=0;
memset(dfn,0,sizeof(dfn));
memset(cut,0,sizeof(dfn));
for(int i=1;i<=n;i++)
if(!dfn[i])
{
sum++;
s=0;
root=i;
tarjan(i,-1);
num[i]=s;
} }
int main()
{
int a,b,n,m,i;
while(scanf("%d%d",&n,&m),n||m)
{
init();
while(m--)
{
scanf("%d%d",&a,&b);
a++;
b++;
add(a,b);
add(b,a);
}
solve(n);
int ans=0;
int tep=-1;
for(i=1;i<=n;i++)
{
if(cut[i])
{
if(ans<cut[i]+1)
{
ans=cut[i]+1;
tep=i;
}
}
}
//注意:当所有联通分量的元素个数都是1的时候去掉一个元素则联通分量减小
if(tep==-1)
{
int flag=0;
for(i=1;i<=n;i++)
{
if(num[i]>1)
flag++;
}
if(flag)
printf("%d\n",sum);
else
printf("%d\n",sum-1);
}
else
printf("%d\n",ans+sum-1);
}
}
无向连通图求割点(tarjan算法去掉改割点剩下的联通分量数目)的更多相关文章
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- 图连通性【tarjan点双连通分量、边双联通分量】【无向图】
根据 李煜东大牛:图连通性若干拓展问题探讨 ppt学习. 有割点不一定有割边,有割边不一定有割点. 理解low[u]的定义很重要. 1.无向图求割点.点双联通分量: 如果对一条边(x,y),如果low ...
- 洛谷3388 【模板】割点 tarjan算法
题目描述 给出一个n个点,m条边的无向图,求图的割点. 关于割点 在无向连通图中,如果将其中一个点以及所有连接该点的边去掉,图就不再连通,那么这个点就叫做割点(cut vertex / articul ...
- 割点 —— Tarjan 算法
由于对于这一块掌握的十分不好,所以在昨天做题的过程中一直困扰着我,好不容易搞懂了,写个小总结吧 qwq~ 割点 概念 在无向连通图中,如果将其中一个点以及所有连接该点的边去掉,图就不再连通,那么这个点 ...
- 『Tarjan算法 无向图的割点与割边』
无向图的割点与割边 定义:给定无相连通图\(G=(V,E)\) 若对于\(x \in V\),从图中删去节点\(x\)以及所有与\(x\)关联的边后,\(G\)分裂为两个或以上不连通的子图,则称\(x ...
- ZOJ 2588 Burning Bridges(无向连通图求割边)
题目地址:ZOJ 2588 由于数组开小了而TLE了..这题就是一个求无向连通图最小割边.仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大 ...
- ZOJ2588:Burning Bridges(无向连通图求割边)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 吐下槽,不得不说ZOJ好坑,模版题做了一个多小时. 题意:* ...
- Tarjan 算法求 LCA / Tarjan 算法求强连通分量
[时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...
- 有向图的强联通tarjan算法(判断是否为强联通模板)(hdu1269)
hdu1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- View与Model绑定注意事项 (视图无数据显示)
Qt 中视图与模型绑定时,模型必须使用new来创建.否则刚开始初始化的时候,视图无数据显示,或者后期视图不能随着模型的改变而改变. 具体原因:我猜测是局部变量生命周期的问题.new 的变量在堆中,除非 ...
- j2se j2ee j2me
多数编程语言都有预选编译好的类库以支持各种特定的功能,在Java中,类库以包(package)的形式提供,不同版本的Java提供不同的包,以面向特定的应用. Java2平台包括标准版(J2SE).企业 ...
- thinkPHP的优缺点
适合大量重复的工作,但不太灵活...
- ffmpeg笔记——UDP组播接收总结
ffmpeg在avformat_open_input里面已经实现了UDP的协议,所以只需要设置好参数,将url传递进去就可以了. 和打开文件的方式基本一样: 01 AVCodecContext *pV ...
- java反射详解 (转至 http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html)
本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案例1]通过一个对象 ...
- 转载:15个最受欢迎的Python开源框架
出自:http://python.jobbole.com/72306/?replytocom=57112 15个最受欢迎的Python开源框架 Django: Python Web应用开发框架 Dja ...
- is_file,is_dir,file_exists
is_file()和file_exists()效率比较,结果当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当.同理,当文件目录存在时,is_dir ...
- MySQL数据库行去重复
1.创立数据表
- [转]线上GC故障解决过程记录
排查了三四个小时,终于解决了这个GC问题,记录解决过程于此,希望对大家有所帮助.本文假定读者已具备基本的GC常识和JVM调优知识,关于JVM调优工具使用可以查看我在同一分类下的另一篇文章: http: ...
- css之鼠标cursor
<html> <body> <p>请把鼠标移动到单词上,可以看到鼠标指针发生变化:</p> <span style="cursor:au ...