POJ 1523 SPF 图的割点

本文介绍了一种用于检测网络中单点故障(SPF)节点的算法。SPF节点是指一旦失效,将导致原本完全连通的网络中至少有一对节点无法通信的节点。文章详细描述了算法的实现思路,包括如何确定节点的失败会留下多少子网,以及两种实现方法:邻接表深度优先搜索和邻接矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SPF

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10726 Accepted: 4770

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

Source

Greater New York 2000

思想:当前节点(cur)搜索访问的顺序num[cur]与后继节点next不通过cur所能访问的最小的节点搜索的值low[next].

low[cur]=\begin{Bmatrix} min(low[cur],low[next]) & & & next & is & visited & & & & & & & & & & & & \\ min(low[cur],low[next]) & & & next & is &not& visited & & & & & \end{Bmatrix}

如果 low[cur]>=num[father],即low不能通过father不能到达更小的节点,则father为割点;当cur=root时,判断cur的child>=2,即为割点。

方法一:邻接表DFS,O(N+M);

#include <iostream>
#include <vector>
using namespace std;
#define M 1001
vector<int> e[M];
int num[M],low[M],mark[M];
int large,root,index;
void dfs(int cur,int f){
	int child=0;
	num[cur]=low[cur]=++index;
	for(int j=0;j<e[cur].size();j++){
		int i=e[cur][j];
		if(!num[i]){
			child++;
			dfs(i,cur); 
			low[cur]=min(low[cur],low[i]);
			mark[cur]+= cur==1 ? child>1 : low[i]>=num[cur];  
		}else if(i!=f){
			low[cur]=min(low[cur],num[i]); 
		}
	}
}

int main(int argc, char *argv[]) {
	int a,b,cnt=0,flag=2;
	while(1){
		large=-1,cnt++,index=0;
		cin>>a;
		if(!a)
			break;
		else
			cin>>b;
		for(int i=1;i<=M;i++){
			mark[i]=low[i]=num[i]=0;
			e[i].clear();
		}		
		while(1){
			large=max(a,large);
			large=max(large,b);
			e[a].push_back(b);
			e[b].push_back(a); 
			cin>>a;
			if(a==0)
				break;				
			cin>>b;			
		}		
		root=1;
		dfs(1,root);
		cout<<"Network #"<<cnt<<endl;
		int has=false;
		for(int i=1;i<=large;i++){
			if(mark[i]){
				cout<<"  SPF node "<<i<<" leaves "<<mark[i]+1<<" subnets"<<endl;
				has=true;
			}
		}
		if(!has)
			cout<<"  No SPF nodes"<<endl;
		cout<<endl;
	}
	return 0;
}

方法二:邻接矩阵,O(n*n);

#include <iostream>
using namespace std;
#define M 1001
int e[M][M],num[M],low[M],mark[M],large,root,index;
void dfs(int cur,int f){
	int child=0;
	num[cur]=low[cur]=++index;
	for(int i=1;i<=large;i++){
		if(e[cur][i]){
			if(!num[i]){
				child++;
				dfs(i,cur); 
				low[cur]=min(low[cur],low[i]);
				//cur点已经深度更新low,判断low 
				mark[cur]+= cur==1 ? child>1 : low[i]>=num[cur]; 
			//不通过父节点所直接到达的index  
			}else if(i!=f){
				low[cur]=min(low[cur],num[i]); 
			}
		}
	}
}

int main(int argc, char *argv[]) {
	int a,b,cnt=0,flag=2;
	while(1){
		large=-1,index=0;
		cnt++;
		cin>>a;
		if(!a)
			break;
		else
			cin>>b;
		for(int i=1;i<=M;i++){
			mark[i]=low[i]=num[i]=0;
			for(int j=1;j<M;j++)
				e[i][j]=0;
		}		
		while(1){
			large=max(a,large);
			large=max(large,b);
			e[a][b]=e[b][a]=1;
			cin>>a;
			if(a==0)
				break;				
			cin>>b;			
		}		
		root=1;
		dfs(1,root);
		cout<<"Network #"<<cnt<<endl;
		bool has=false;
		for(int i=1;i<=large;i++){
			if(mark[i]){
				cout<<"  SPF node "<<i<<" leaves "<<mark[i]+1<<" subnets"<<endl;
				has=true;
			}
		}
		if(!has)
			cout<<"  No SPF nodes"<<endl;
        //每个例子之间有空行!!!!
		cout<<endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值