POJ1523(割点所确定的连用分量数目,tarjan算法原理理解)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7406 | Accepted: 3363 |
Description
Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.

Input
Output
The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.
Sample Input
1 2
5 4
3 1
3 2
3 4
3 5
0 1 2
2 3
3 4
4 5
5 1
0 1 2
2 3
3 4
4 6
6 3
2 5
5 1
0 0
Sample Output
Network #1
SPF node 3 leaves 2 subnets Network #2
No SPF nodes Network #3
SPF node 2 leaves 2 subnets
SPF node 3 leaves 2 subnets
题意:问将某个点删除可产生多少个连通分量。
思路:考察对tarjan算法原理理解,解释见代码。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Edge{
int to,next;
}es[MAXN*];
int V,E;
int head[MAXN];
inline int max(int u,int v)
{
return u > v? u: v;
}
inline int min(int a,int b)
{
return a > b? b: a;
}
void add_edge(int u,int v)
{
es[E].to=v;
es[E].next=head[u];
head[u]=E++;
V=max(max(u,v),V);
}
bool flag;
int root;
int subnets[MAXN];
int index;
int dfn[MAXN],low[MAXN];
void tarjan(int u,int fa)
{
int son=;
dfn[u]=low[u]=++index;
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(!dfn[v])
{
tarjan(v,u);
son++;
low[u]=min(low[u],low[v]);
if((u==root&&son>)||(u!=root&&dfn[u]<=low[v]))
{
flag=true;
subnets[u]++;
//u->v 该边导致u成为割点
//当dfn[u]==low[v]时u->v为返祖边,u、v处于同一双连通分量中
//当dfn[u]<low[v]时u->v为割边
//删除割点u产生的连通数目为:u所在的连通分量数目+与u所连接的割边的数目+1(边:fa->u)
}
}
else if(v!=fa) low[u]=min(low[u],dfn[v]);
}
}
int main()
{
int cas=;
int u,v;
while(true)
{
v=-;
index=;
memset(subnets,,sizeof(subnets));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
V=-,E=;
flag=false;
while(scanf("%d",&u)&&u)
{
scanf("%d",&v);
add_edge(u,v);
add_edge(v,u);
}
if(v==-) break;
root=V;
tarjan(root,-);
printf("Network #%d\n",++cas);
if(flag)
{
for(int i=;i<=V;i++)
{
if(subnets[i]>)
{
printf(" SPF node %d leaves %d subnets\n",i,subnets[i]+);//加上fa->u该边所连接的连通分量
}
}
}
else printf(" No SPF nodes\n");
printf("\n");
}
return ;
}
POJ1523(割点所确定的连用分量数目,tarjan算法原理理解)的更多相关文章
- 寻找图的强连通分量:tarjan算法简单理解
1.简介tarjan是一种使用深度优先遍历(DFS)来寻找有向图强连通分量的一种算法. 2.知识准备栈.有向图.强连通分量.DFS. 3.快速理解tarjan算法的运行机制提到DFS,能想到的是通过栈 ...
- 有向图强连通分量的Tarjan算法
有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...
- 【转】有向图强连通分量的Tarjan算法
原文地址:https://www.byvoid.com/blog/scc-tarjan/ [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly con ...
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...
- 【转载】有向图强连通分量的Tarjan算法
转载地址:https://www.byvoid.com/blog/scc-tarjan [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly conn ...
- 有向图强连通分量的Tarjan算法(转)
[有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...
- 强连通分量的Tarjan算法
资料参考 Tarjan算法寻找有向图的强连通分量 基于强联通的tarjan算法详解 有向图强连通分量的Tarjan算法 处理SCC(强连通分量问题)的Tarjan算法 强连通分量的三种算法分析 Tar ...
- 『图论』有向图强连通分量的Tarjan算法
在图论中,一个有向图被成为是强连通的(strongly connected)当且仅当每一对不相同结点u和v间既存在从u到v的路径也存在从v到u的路径.有向图的极大强连通子图(这里指点数极大)被称为强连 ...
- 有向图强连通分量的Tarjan算法及模板
[有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强联通(strongly connected),如果有向图G的每两个顶点都强联通,称有向图G是一个强联通图.非强联通图有向 ...
随机推荐
- Android 事件分发机制 图解
在Android 开发中事件分发是比较重要的,也是比较难理解的,之前看过这方面的东西,以为自己弄懂了,也就没太注意,最近面试呢,想着肯定要问到这一块的东西,回顾的时候发现又忘了,真是好记性不如烂笔头啊 ...
- python 基础 8.1 r 正则对象
...
- python发布IIS
参考文档 https://segmentfault.com/a/1190000008909201 http://blog.51cto.com/anngle/1922041 https://www.cn ...
- 50道JAVA基础编程练习题 - 题目
50道JAVA基础编程练习题[1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? [2]题目:判断 ...
- 九度OJ 1048:判断三角形类型 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6794 解决:3361 题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输 ...
- 关于maven的profile
1 什么是profile profile本质上就是不同的环境对应不同的配置. 这样的好处是,在命令行中指定具体的profile的时候,会有自己独特的参数或者独特的配置来为不同的环境生成不同的目标代码. ...
- svn服务的安装和使用
更新linux软件库 cat /etc/redhat-release wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.c ...
- 【题解】P3258松鼠的新家
[题解][P3258 JLOI2014]松鼠的新家 树链剖分板子题. 总结一点容易写错的地方吧: if(d[top[u]]<d[top[v]]) swap(u,v);注意是\(top\). 在\ ...
- python的类型
弱类型是可以自由转换的,如js,字符串和数字能相加 强类型不能自由转换,如python,要加上函数转成相同的类型
- 在win7下使用git和gitlab进行code review
1.安装 Git-2.6.3-64-bit.exe 下载地址:http://pan.baidu.com/s/1hqGvwnq 2.根据收到的邮件进入gitlab网站,并修改密码登陆 3.新建一个文件 ...