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

题干:

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 ;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440140.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习笔记(2):单变量线性回归

目录 1&#xff09;Model representation 2&#xff09;Cost function 3&#xff09;Cost function intuition 1 4&#xff09;Cost function intuition2 5&#xff09;Gradient descent 6&#xff09;Gradient descent intuition 7&#xff09;Gradient descent for li…

安装VMware tools

点击“虚拟机” 安装VMware tools提取图中文件到“下载” 提取登入root 进入 cd 下载/vmware-tools-distrib 执行 ./vmware-install-pl 输入yes或者点击“enter”出现图中&#xff0c;即为成功安装

Keras入门实战(1):MNIST手写数字分类

目录 1)首先我们加载Keras中的数据集 2&#xff09;网络架构 3&#xff09;选择编译(compile参数) 4&#xff09;准备图像数据 5) 训练模型 6&#xff09;测试数据 前面的博客中已经介绍了如何在Ubuntu下安装Keras深度学习框架。 现在我们使用 Keras 库来学习手写数字分…

【BZOJ - 2574】[Poi1999] Store-Keeper(点双连通分量,求割点,记忆化bfs)

题干&#xff1a; 有一个仓库被分成n*m 个矩形区域&#xff0c;如果两个区域有一条公共边&#xff0c;则被认为这两个区域相邻。包裹都放在一个区域中&#xff0c;剩余的区域或者空闲或者被集装箱占有&#xff0c;这是因为集装箱太重&#xff0c;仓库管理员不能将集装箱搬走。…

机器学习笔记(3):线性代数回顾

目录 1&#xff09;Matrices and vectors 2&#xff09;Addition and scalar multiplication 3&#xff09;Matrix-vector multiplication 4&#xff09;Matrix-matrix multiplication 5&#xff09;Matrix multiplication properties 6&#xff09;Inverse and transpos…

hadoop 安装

Hadoop单机和伪分布式安装 更新apt 用root用户登录 先更新一下 apt apt-get update然后安装vim apt-get install vim安装VMware tools tools 安装 安装SSH、配置SSH无密码登陆 单节点模式都需要用到 SSH 登陆&#xff0c;Ubuntu 默认已安装了 SSH client&#xff0c;此…

机器学习笔记(4):多变量线性回归

目录 1&#xff09;Multiple Features 2&#xff09;Gradient descent for multiple variables 3&#xff09;Gradient descent in practice 1: Feature Scaling 4&#xff09;Gradient descent in pratice2: Learning rate 5&#xff09;Features and polynomial regress…

zookeeper单节点部署

hadoop 安装 在/install-package目录下查看zookeeper的安装包 本文中安装的是zookeeper-3.4.12.tar.gz 下方为百度云链接 链接&#xff1a;https://pan.baidu.com/s/1bzq4ILH41owtS__3tBCcRQ 提取码&#xff1a;6q4r 把下载好的zookeeper-3.4.12.tar.gz 放到/install-packa…

机器学习笔记(五):逻辑回归

目录 1&#xff09;Classification 2&#xff09;Hypothesis Representation 3&#xff09;Decision boundary 4&#xff09;Cost function 5&#xff09;Simplified cost function and gradient descent 6&#xff09;Multi-class classification:One-vs-all 7&#xf…

xrdp完美实现Windows远程访问Ubuntu 16.04

前言&#xff1a; 在很多场景下&#xff0c;我们需要远程连接到Linux服务器(本文是Ubuntu)&#xff0c;传统的连接主要分为两种。 第一种&#xff1a;通过SSH服务&#xff08;使用xshell等工具&#xff09;来远程访问&#xff0c;编写终端命令&#xff0c;不过这个是无界面的&a…

机器学习笔记(六):正则化

目录 1&#xff09;The problem of overfitting 2&#xff09;Cost function 3&#xff09;Regularized linear regression 4&#xff09;Regularized logistic regression 我们已经学习了线性回归和逻辑回归算法&#xff0c;已经可以有效解决很多问题&#xff0c;但是在实…

Hbase单节点安装

zookeeper单节点部署 实验环境 操作系统&#xff1a;Ubuntu 16.04 Hadoop&#xff1a;Hadoop 2.7.5 Zookeeper&#xff1a;zookeeper 3.4.12 Java&#xff1a;java version 1.8.0 到/install-package目录下查看hbase安装包 #>ls /install-package本文中用的是hbase-1…

机器学习笔记(七):神经网络:表示

目录 1&#xff09;Non-linear hypotheses 2&#xff09;Model representation 1 3&#xff09;Model representation 2 4&#xff09;Examples and intuitions 1 5&#xff09;Examples and intuitions 2 6&#xff09;Multi-class classification 1&#xff09;Non-lin…

ROS入门_1.10 理解ROS服务和参数

目录 ROS Services使用rosservice rosservice listrosservice typerosservice call Using rosparam rosparam listrosparam set and rosparam getrosparam dump and rosparam load 本教程假设从前一教程启动的turtlesim_node仍在运行&#xff0c;现在我们来看看turtlesim提供了…

1.Introduction and Evaluation

感谢七月在线罗老师和吴同学&#xff01; 最近报了七月在线的《推荐系统实战》班&#xff0c;根据上课资料和思维导图整理了这篇笔记&#xff01; 1&#xff09;推荐系统介绍 思维导图如下&#xff0c;其中需要掌握的是推荐系统存在的前提&#xff1a;信息过载和用户需求不明…

【ZOJ - 2968 】Difference Game (贪心,思维模拟)

题干&#xff1a; Now you are going to play an interesting game. In this game, you are given two groups of distinct integers and C coins. The two groups, named Ga and Gbrespectively, are not empty and contain the same number of integers at first. Each time…

使用 rqt_console 和 roslaunch

Description:本教程介绍如何使用 rqt_console 和 rqt_logger_level 进行调试&#xff0c;以及如何使用 roslaunch 同时运行多个节点。早期版本中的 rqt 工具并不完善&#xff0c;因此&#xff0c;如果你使用的是“ROS fuerte”或更早期的版本&#xff0c;请同时参考 这个页面 学…

机器学习必备宝典-《统计学习方法》的python代码实现、电子书及课件

本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 6天前 《统计学习方法》可以说是机器学习的入门宝典&#xff0c;许多机器学习培训班、互联网企业的面试、笔试题目&#xff0c;很多都参考这本书。本站根据网上资料用python复现…

【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/881/H 来源&#xff1a;牛客网 Bobo has a set A of n integers a1,a2,…,ana1,a2,…,an. He wants to know the sum of sizes for all subsets of A whose xor sum is zero modulo (1097)(1097). F…

机器学习入门必备的13张“小抄”(附下载)

目录 1&#xff09;TensorFlow 2&#xff09;Keras 3&#xff09;Neural Networks 4&#xff09;Numpy 5&#xff09;Scipy 6&#xff09;Pandas 7&#xff09;Scikit-learn 8&#xff09;Matplotlib 9&#xff09;PythonForDataScience 最近在github上发现了很有用的…