结合tarjan算法思想,这题终于写了出来。

同样用dfs将图变成为一颗树,这样可以提供许多有用的性质。

对于一个无向连通图,dfs后的树为只有回边(回边Euv,v是u的祖先)和生成树的边的图。 那么在遍历到一个点u的时候,可以知道如果不考虑这个点,如果与u相邻的点连通那么u不是割点,否则是割点。 那么只需要判断与u相邻的点是否连通就行了,于是借鉴tarjan求强连通的办法,在dfs时,对每个点标记一个深度low[N]也就是从根到这个点最短路径(经过的最小结点数), 然后在遍历到u点的时候,看看与u相邻的点v的low[v], 如果low[U] >= low[u]那么说明u就是割点.  因为v点无法到达u相邻的某些点。 当然当u为根节点的时候要特判

SPF
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4366   Accepted: 2009

Description

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

Source

Greater New York 2000
// start time 12:44
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define N 1010
#define INF 0x3ffffff
int g[N][N];
int mark[N];
int low[N];
int save[N];
int n;void dfs(int s,int cnt)
{low[s]=cnt;int mi=INF;int k=0;for(int i=1;i<=n;i++){if(g[s][i]==0||s==i) continue;if(low[i] == -1){dfs(i,cnt+1);if( low[i] >= cnt ) k++;}mi=min(low[i],mi);}low[s]=mi;if(cnt==0) k--;if(k!=0) save[s]=k;
}int main()
{int tt=1;int x,y;while(scanf("%d",&x)&&x){memset(mark,0,sizeof(mark));mark[x]=1;n=0;if(x>n) n=x;memset(g,0,sizeof(g));memset(save,-1,sizeof(save));memset(low,-1,sizeof(low));scanf("%d",&y);mark[y]=1;if(y>n) n=y;g[x][y]=g[y][x]=1;while(scanf("%d",&x)&&x){mark[x]=1;if(x>n) n=x;scanf("%d",&y);mark[y]=1;if(y>n) n=y;g[x][y]=g[y][x]=1;}for(int i=1;i<=n;i++)if(mark[i]==1){dfs(i,0);break;}printf("Network #%d\n",tt++);int flag=0;for(int i=1;i<=n;i++){if(save[i]!=-1){flag=1;printf("  SPF node %d leaves %d subnets\n",i,save[i]+1);}}if(flag==0)printf("  No SPF nodes\n");printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/05/13/3075662.html

poj 1523(无向联通图的割点)相关推荐

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

    题干: Consider the two networks shown below. Assuming that data moves around these networks only betwe ...

  2. Tarjan点的双联通(寻找割点)

    问题概述:给你一个无向联通图,你要在图中标记一些点,使得这个图中的任意一个点消失了,剩余的点都可以通 过一条路径到达你某个标记的点.问你最少需要选择多少个点,并且在最优的情况下有多少总选点方案,(每个 ...

  3. 001.Tarjan算法:求解图的割点与桥(割边)

    简介: 割边和割点的定义仅限于无向图中.我们可以通过定义以蛮力方式求解出无向图的所有割点和割边,但这样的求解方式效率低.Tarjan提出了一种快速求解的方式,通过一次DFS就求解出图中所有的割点和割边 ...

  4. POJ 1523 (割点+连通分量)

    题目链接: http://poj.org/problem?id=1523 题目大意:连通图,找图中割点,并计算切除该割点后,图中的连通分量个数. 解题思路: POJ的数据很弱. Tarjan法求割点. ...

  5. 图的割点、桥与双连通分支

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

  6. 图的割点(边表集实现)

    /*     Name: 图的割点(边表集实现)     Copyright:      Author: 巧若拙      Date: 20-11-14 21:17     Description:  ...

  7. [转载]图的割点、桥与双连通分支

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

  8. 图的割点(解释及实现)

    图的割点 1.割点是什么 2.找割点的办法 2.1方法的核心 2.2 具体过程 2.3实现 1.割点是什么 在一个无向连通图中,如果删除某个顶点后,图不再连通(任意两顶点之间不能相互到达),我们称这样 ...

  9. 图的割点 桥 双连通(byvoid)

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

最新文章

  1. 免费直播:主流深度框架对比:总有一款适合你~
  2. 2015第27周一非结构化数据
  3. 批量图片压缩工具:JPGCompact 2.0绿色版
  4. Android 必须知识 PWA Android Instant Apps
  5. Linux fork() 和 vfork()
  6. Go web framework
  7. 【域控管理】父域的搭建
  8. html 手机端无法拖动地图,关于腾讯地图api的禁止地图拖动问题
  9. 套接字编程---2(TCP套接字编程的流程,TCP套接字编程中的接口函数,TCP套接字的实现,TCP套接字出现的问题,TCP套接字多进程版本,TCP套接字多线程版本)
  10. UCan下午茶武汉站,为你全面挖宝分布式存储
  11. python程序化 k线指定时间更新_Python获取股票历史、实时数据与更新到数据库
  12. 如何安装ArchLinux
  13. 【最新最全】JavaScript从入门到精通_Web前端必学的JS教程
  14. 传承百年经典的瑞吉管家静待您的优雅旅程再次开启
  15. Linux网络配置(NAT模式)
  16. ufvm可以读哪些网格_FM24C05UFVM8
  17. 【vue】Element Calendar 组件显示农历及节日
  18. 学习WPF之解决方案和项目结构
  19. C++编译错误与运行时错误
  20. python分支结构if详解

热门文章

  1. 简单介绍互联网领域选择与营销方法
  2. 2022-2028年中国激光脱毛仪行业市场研究及前瞻分析报告
  3. Go 学习笔记(68)— goroutine 并发控制神器 Context
  4. fatal error all goroutines are asleep - deadlock!
  5. 【软件工程】RUP与软件开发5大模型
  6. 卷积核输出特征图大小的计算 深度学习
  7. 通俗理解tf.nn.conv2d() tf.nn.conv3d( )参数的含义 pytorhc 卷积
  8. 对装饰器@wraps的解释(一看就懂)-- 并对装饰器详解
  9. FuzzyCMeans算法
  10. 自动驾驶与汽车安全电子技术