POJ 1523、ZOJ 1119 SPF - from lanshui_Yang
来源:互联网 发布:热血传奇装备数据库 编辑:程序博客网 时间:2024/06/05 11:49
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.
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.
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 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100
Sample Output
Network #1 SPF node 3 leaves 2 subnetsNetwork #2 No SPF nodesNetwork #3 SPF node 2 leaves 2 subnets SPF node 3 leaves 2 subnets
题目大意:给你一个连通图,让你判断去掉某个点及其关联的边后剩下的图是否连通,如果不连通,计算出剩下的连通分支个数。
解题思路:这道题是典型的找无向图的关节点问题,顶点u是是关节点的充要条件如下:(1)如果顶点u是深度优先搜索生成树根,则u至少有两个子女。(2)如果u不是生成树的根,则它至少有一个子女w,从w出发,不可能通过w、w的子孙,以及一条回边(最多是一条)组成的路径到达u的祖先。
代码如下:
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std ;const int MAXN = 1005 ;struct arcNode{ int adj ; arcNode * next ;} ;arcNode *vert[MAXN] ;bool vis[MAXN] ; // 建立标记数组int low[MAXN] ; // 记录从u或u的子孙出发(可以通过回边)可以到达的最低深度优先数int dfn[MAXN] ; // 记录深度优先数int sumfz[MAXN] ; // 记录去掉某点及其相关联的边后,剩余的连通分量个数int son ; // 记录树根的孩子个数int tmpdfn ;int sumnode ; //记录出现的结点的最大序号int ca = 0 ;void dfs(int u) // 找关节点{ arcNode * p = vert[u] ; while (p != NULL) { int v = p -> adj ; if(!vis[v]) { vis[v] = 1 ; tmpdfn ++ ; dfn[v] = low[v] = tmpdfn ; dfs(v) ; low[u] = min(low[u] , low[v]) ; if(low[v] >= dfn[u]) { if(u == 1) { son ++ ; } else { sumfz[u] ++ ; } } } else { low[u] = min(dfn[v] , low[u]) ; // 千万注意:这里是dfn(v) 不是,low(v) } p = p -> next ; }}void dele(){ arcNode *p ; int i ; for(i = 1 ; i <= sumnode ; i ++) { p = vert[i] ; while (p != NULL) { vert[i] = p -> next ; delete p ; p = vert[i] ; } }}void init(){ int a , b ; while (scanf("%d" , & a) != EOF) { sumnode = 0 ; tmpdfn = 0 ; memset(vert , 0 , sizeof(vert)) ; if(a == 0) break ; scanf("%d" , &b) ; arcNode *p ; if(a > sumnode) sumnode = a ; if(b > sumnode) sumnode = b ; p = new arcNode ; p -> adj = b ; p -> next = vert[a] ; vert[a] = p ; p = new arcNode ; p -> adj = a ; p -> next = vert[b] ; vert[b] = p ; while (scanf("%d", &a) != EOF) { if(a == 0) break ; scanf("%d" , &b) ; if(a > sumnode) sumnode = a ; if(b > sumnode) sumnode = b ; p = new arcNode ; p -> adj = b ; p -> next = vert[a] ; vert[a] = p ; p = new arcNode ; p -> adj = a ; p -> next = vert[b] ; vert[b] = p ; } son = 0 ; memset(vis , 0 , sizeof(vis)) ; memset(dfn , 0 , sizeof(dfn)) ; memset(low , 0 , sizeof(low)) ; memset(sumfz , 0 , sizeof(sumfz)) ; vis[1] = 1 ; dfn[1] = low[1] = 1 ; tmpdfn = 1 ; dfs(1) ; // 这里默认点1是根节点 if(son > 1) sumfz[1] = son ; if(ca > 0) puts("") ; printf("Network #%d\n" , ++ ca) ; int i ; int pan = 0 ; for(i = 1 ; i <= sumnode ; i ++) { if(sumfz[i] > 0) { pan = 1 ; if(i != 1) printf(" SPF node %d leaves %d subnets\n" , i , sumfz[i] + 1) ; else printf(" SPF node %d leaves %d subnets\n" , i , sumfz[i]) ; } } if(pan == 0) { puts(" No SPF nodes") ; } dele() ; // 释放图 }}int main(){ init() ; return 0 ;}
- POJ 1523、ZOJ 1119 SPF - from lanshui_Yang
- poj 1523 | zoj 1119 SPF
- POJ 2418 ,ZOJ 1899 Hardwood Species - from lanshui_Yang
- POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang
- POJ Chores - from lanshui_Yang
- POJ 2080 Calender -- from lanshui_Yang
- ZOJ 1649 Rescue (BFS) -- from lanshui_Yang
- ZOJ - 1119 SPF
- POJ 2081 Recaman's Sequence -- from lanshui_Yang
- POJ 1519 Digital Roots -- from lanshui_Yang
- POJ 3749 破译密码 --from lanshui_Yang
- POJ 2389 Bull Math --from lanshui_Yang
- POJ 2501 Average Speed --from lanshui_Yang
- POJ 1300 Door Man - from lanshui_Yang
- POJ 2513 Colored Sticks - from lanshui_Yang
- POJ 3177 Redundant Paths - from lanshui_Yang
- POJ 2186 Popular Cows - from lanshui_Yang
- POJ 1904 King's Quest - from lanshui_Yang
- IP加速器 加速各种网络游戏.IP加速器构建的高速虚拟网络
- 项目管理之沟通和计划
- 单独启动tomcat
- 强连通分量Tarjan模板
- Window/.Net程序员如何用最简单的方式掌握Linux的本质
- POJ 1523、ZOJ 1119 SPF - from lanshui_Yang
- hdu4616 树形dp,分治思想
- 【解题报告】POJ 3270 Cow 置换群基础 -- 轮换
- 【原】常见CSS3属性对ios&android&winphone的支持
- C语言生产者与消费者-随机数实现
- nodejs+express+socketio实现即时聊天系统初体验
- Unity3d Transform.DetachChildren使用
- @Local注释和@Remote注释不能一起使用
- hdu 1505 City Game (hdu 1506的升级版)