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是一个强联通图.非强联通图有向 ...
随机推荐
- centos安装python3.7.0过程记录
参考自这里,整理出以下步骤. 一.下载python3.7.0包地址:https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 二.安装 登陆Li ...
- HDFS源码分析之UnderReplicatedBlocks(二)
UnderReplicatedBlocks还提供了一个数据块迭代器BlockIterator,用于遍历其中的数据块.它是UnderReplicatedBlocks的内部类,有三个成员变量,如下: // ...
- 利用socket.io实现多人聊天室(基于Nodejs)
socket.io简单介绍 在Html5中存在着这种一个新特性.引入了websocket,关于websocket的内部实现原理能够看这篇文章.这篇文章讲述了websocket无到有,依据协议,分析数据 ...
- 微信小程序配置详解
在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...
- svn 出现冲突时可以使用 meld . 命令合并。 而git的冲突合并详见内容
1.可以在任意目录使用 git mergetool --tool-help 查看 git 所支持的merge tools. 2.可以使用如下配置去设置merge tool 和 diff tool ...
- 超限学习机 (Extreme Learning Machine, ELM) 学习笔记 (一)
1. ELM 是什么 ELM的个人理解: 单隐层的前馈人工神经网络,特别之处在于训练权值的算法: 在单隐层的前馈神经网络中,输入层到隐藏层的权值根据某种分布随机赋予,当我们有了输入层到隐藏层的权值之后 ...
- python -- redis连接与使用
前面我们简单介绍了redis nosql数据库,现在我们在python里面来使用redis. 一.python连接redis 在python中,要操作redis,目前主要是通过一个python-red ...
- ZOJ - 3930 Dice Notation 【模拟】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3930 题意 给出一串字符串 如果是 '+' '-' '*' '/ ...
- 《高性能Javascript》 Summary(二)
第四章.算法和流程控制 Algorithms And Flow Control 原因:代码整体结构是执行速度的决定因素之一.代码量少不一定运行速度快,代码量多不一定运行速度慢.性能损失与组织代码和具体 ...
- STM32 Flash Download failed
笔者使用Keil开发环境对STM32L Discovery进行开发,更新一次firmware后就不能连接目标板了,报错Internal command error Flash download fai ...