【POJ】1523 SPF 割点

来源:互联网 发布:和人工智能聊天的软件 编辑:程序博客网 时间:2024/05/01 11:46
SPF
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 5855
Accepted: 2693

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

Source

Greater New York 2000

传送门:【POJ】1523 SPF

题目分析:求割点的模板题,tarjan一次得到所有的割点,然后对每个割点的儿子dfs一遍,dfs的次数就是删除该割点能得到的连通块的数量。

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define clear( a , x ) memset ( a , x , sizeof a )const int MAXN = 1005 ;const int MAXE = 1000000 ;struct Edge {int v , n ;Edge ( int var = 0  , int next = 0 ) : v ( var ) , n ( next ) {}} ;struct BCC {Edge edge[MAXE] ;int adj[MAXN] , cntE ;int cut[MAXN] ;int low[MAXN] , dfn[MAXN] , dfs_clock ;bool vis[MAXN] ;void init () {cntE = dfs_clock = 0 ;clear ( cut , 0 ) ;clear ( dfn , 0 ) ;clear ( adj , -1 ) ;}void addedge ( int u , int v ) {edge[cntE] = Edge ( v , adj[u] ) ;adj[u] = cntE ++ ;edge[cntE] = Edge ( u , adj[v] ) ;adj[v] = cntE ++ ;}void tarjan ( int u , int fa ) {low[u] = dfn[u] = ++ dfs_clock ;int son = 0 ;for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( !dfn[v] ) {++ son ;tarjan ( v , u ) ;low[u] = min ( low[u] , low[v] ) ;if ( low[v] >= dfn[u] )cut[u] = 1 ;}else if ( v != fa )low[u] = min ( low[u] , dfn[v] ) ;}if ( fa < 0 && son == 1 )cut[u] = 0 ;}void dfs ( int u ) {vis[u] = 1 ;for ( int i = adj[u] ; ~i ; i = edge[i].n )if ( !vis[edge[i].v] )dfs ( edge[i].v ) ;}void solve () {int flag = 0 ;REP ( u , MAXN ) {if ( cut[u] ) {flag = 1 ;int num = 0 ;clear ( vis , 0 ) ;vis[u] = 1 ;for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( !vis[v] ) {++ num ;dfs ( v ) ;}}printf ( "  SPF node %d leaves %d subnets\n" , u , num ) ;}}if ( !flag )printf ( "  No SPF nodes\n" ) ;}} ;BCC c ;int input () {c.init () ;int u , v ;int flag = 0 ;while ( scanf ( "%d" , &u ) ) {if ( !u ) {if ( !flag )return 0 ;elsereturn 1 ;}flag = 1 ;scanf ( "%d" , &v ) ;c.addedge ( u , v ) ;}return 0 ;}void work () {int cas = 0 ;while ( input () ) {printf ( "Network #%d\n" , ++ cas ) ;REP ( i , MAXN )if ( ~c.adj[i] ) {c.tarjan ( i , -1 ) ;break ;}c.solve () ;printf ( "\n" ) ;}}int main () {work () ;return 0 ;}


0 0