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 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
题目大意:给你一个连通图,让你判断去掉某个点及其关联的边后剩下的图是否连通,如果不连通,计算出剩下的连通分支个数。
解题思路:这道题是典型的找无向图的关节点问题,顶点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 ; }