题干:

Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.

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

The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

Output

For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist.

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
01 2
2 3
3 4
4 5
5 1
01 2
2 3
3 4
4 6
6 3
2 5
5 1
00

Sample Output

Network #1SPF node 3 leaves 2 subnetsNetwork #2No SPF nodesNetwork #3SPF node 2 leaves 2 subnetsSPF node 3 leaves 2 subnets

题目大意:

给你一个联通网路(应该是无自环无回路的),求出这个网络所有割点的编号,以及如果删除这个割点之后所对应的联通分量数。

解题报告:

就是个板子题,,就是输出格式比较坑。。

再就是注意几个地方,一个是if(dfn[x]==0)的时候的low[x]别忘更新,第二是son++要在if(dfn[x]==0)这个判断内。(不然的话就算下面的if(x == rt && son > 1)改成son>2也不行。)

再就是一开始写的时候把init函数放到n=max(a,b)后面了,这样肯定不对啊你都add了两条边了然后又给人家清空了,,还好样例比较良心直接就能发现,不然这个小错又得找半天错。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
struct Edge {int u,v;int ne;
} e[MAX];
int dfn[MAX],low[MAX],clk;
int head[MAX],tot;
int gd[MAX];
int n;
void init() {for(int i = 1; i<=1000; i++) {dfn[i]=low[i]=gd[i]=0;head[i] = -1;}tot = 0;clk = 0;
}
void add(int u,int v) {e[++tot].u = u;e[tot].v = v;e[tot].ne = head[u];head[u] = tot;
}
void tarjan(int x,int fa,int rt) {//注意这里fa和rt是不一样的含义!! dfn[x] = low[x] = ++clk;int son = 0;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].v;if(v == fa) continue;if(dfn[v] == 0) {son++;//放到dfn[v]==0这个if外面也可以??这两种都可以?? 答:不可以 tarjan(v,x,rt);low[x] = min(low[x],low[v]);//别忘这一步啊傻不傻、、if(x != rt && low[v] >= dfn[x]) {gd[x]++;}}else low[x] = min(low[x],dfn[v]);}if(x == rt && son > 1) {gd[x] = son-1;}
}
int main()
{int a,b,iCase=0;while(~scanf("%d",&a) && a) {scanf("%d",&b);init();add(a,b);add(b,a);n=max(a,b); while(~scanf("%d",&a) && a) {scanf("%d",&b);add(a,b);add(b,a);n=max(n,a);n=max(n,b);}tarjan(1,-1,1);printf("Network #%d\n",++iCase);int flag = 0;for(int i = 1; i<=n; i++) {if(gd[i] != 0) printf("  SPF node %d leaves %d subnets\n",i,gd[i]+1),flag = 1;}if(flag == 0) printf("  No SPF nodes\n");printf("\n");}return 0 ;
}

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)相关推荐

  1. poj 1523 SPF (无向图 的 割点)

    http://poj.org/problem?id=1523 题意:  求  无向图的 个点,以及 将个点 去掉后  图 被分成 几个联通块: 题解:  tarjan   .   1 #include ...

  2. POJ 1523 SPF 割点与桥的推断算法-Tarjan

    题目链接: POJ1523 题意: 问一个连通的网络中有多少个关节点,这些关节点分别能把网络分成几部分 题解: Tarjan 算法模板题 顺序遍历整个图,能够得到一棵生成树: 树边:可理解为在DFS过 ...

  3. POJ 1523 SPF (割点 点双连通分量)

    题意:求出割点以及除去割点后的连通分量的数量(附带求出了点双连通分量(块)) [求割点]对图深度优先搜索,定义DFS(u)为u在搜索树(以下简称为树)中被遍历到的次序号.定义Low(u)为u或u的子树 ...

  4. 【132】求把字符串分割成回文串的最少切分次数

    给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文. 返回符合要求的 最少分割次数 . 示例 1: 输入:s = "aab" 输出:1 解释:只需一次分割就可将 s ...

  5. POJ 1523 SPF

    大意:求割顶的数量以及删除割顶之后子图的数量. 思路:Tarjan算法求割顶,同POJ 1144 NetWork. CODE1: #include<cstdio> #include< ...

  6. POJ1523:SPF(无向连通图求割点)

    题目:http://poj.org/problem?id=1523 题目解析: 注意题目输入输入,防止PE,题目就是求割点,并问割点将这个连通图分成了几个子图,算是模版题吧. #include < ...

  7. POJ 1144 Network(无向图连通分量求割点)

    题目地址:POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u不为树根.那么(u,v)为树枝边. ...

  8. Tarjan 算法思想求强连通分量及求割点模板(超详细图解)

    割点定义 在一个无向图中,如果有一个顶点,删除这个顶点及其相关联的边后,图的连通分量增多,就称该点是割点,该点构成的集合就是割点集合.简单来说就是去掉该点后其所在的连通图不再连通,则该点称为割点. 若 ...

  9. [UVA315]Network(tarjan, 求割点)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

最新文章

  1. mysql 1236_Mysql主从同步Last_IO_Errno:1236错误解决方法
  2. macOS新版本终于删掉自带Python2,这波操作连Python死忠粉都叫好
  3. C#中显/隐式实现接口及其访问方法
  4. 规模-复杂世界的简单法则---熵
  5. USB转串口 FT232/PL2303/CH340 驱动以及使用体会
  6. ❤️ 爆肝一个月!JAVA零基础入门总结(下)❤️
  7. 字符集_第07期:有关 MySQL 字符集的 SQL 语句
  8. 世界服务器系统竞赛,他们为何对ASC世界大学生超算竞赛情有独钟?
  9. vmware虚拟化服务器cpu超线程,VMware vSphere的配置方法最佳方案从而提高性能
  10. 在N多气象服务构成的疯狂数据城 AS8000挑起大梁
  11. KMP算法(C语言版)
  12. Reverse Linked List II -- LeetCode
  13. Asterisk的配置详解
  14. 【转】el-cascade设置默认值遇到的坑!
  15. 依码仕喷码机编程指南
  16. 智能交通:电子警察系统技术实施方案(ppt)
  17. 元图地图开放平台系统概述
  18. 四种利用js导出Excel的方法(兼容IE6+、主流浏览器、支持复杂表头和合并单元格)
  19. python 仪表盘监控_做一个基于python的树莓派MCU性能-温度监控仪表盘
  20. 网络安全将是未来10年里面的黄金产业

热门文章

  1. [Leetcode][第216题][JAVA][数组之和3][回溯]
  2. [Leetcode][第733题][JAVA][图像渲染][BFS][DFS]
  3. html代码 打开本地文件,打开本地HTML文件
  4. 计算机修改文字试题,计算机文字处理试题.doc
  5. asm扩容流程_Oracle rac asm 扩容
  6. php正则表达式函数案例,PHP正则表达式函数preg_replace用法实例分析
  7. 6个座位办公室最佳位置_四人办公室座次的首选最佳座位在哪儿
  8. V210 SPI驱动分析
  9. 为什么要自学python_为什么那么多自学Python的后来都放弃了,总结起来就这些原因...
  10. css宋体代码_html布局中统一设置文字字体样式