POJ 1523 SPF(割点所割连通分量数)

本文探讨了如何识别并分析网络中的单点故障(SPF)节点,这些节点在失效时可能导致网络中至少一对可用节点无法进行全连接通信。通过对不同网络结构的分析,展示了SPF节点的存在及其对网络连通性的影响,并提供了检测此类关键节点的方法。

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

题意给你一个无向图,问你该图中有多少割点.且每个割点能把该图分为几个连通分量

思路:令cut[i]为结点i割了之后生成的儿子数目,如果为根节点,那么如果儿子数大于1的话才算是割点,其他的割点最后分割出来的连通分量数为cut[i]+1


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 10005
#define LL long long
int cas=1,T;
int n,m;
int sum = 0;
vector <int> ans;
int dfs_clock;            //时钟,每访问一个结点增1
vector<int>G[maxn];       //图
int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问
int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
bool iscut[maxn];         //标记i结点是不是一个割点
int cut[maxn];            //切割这个结点后把连通块切成多少份
//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥
//初始调用dfs(root,-1)
int dfs(int u,int fa)
{
	int lowu=pre[u]=++dfs_clock;
	int child = 0;                //子结点数目
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		if (!pre[v])
		{
			child++;              //未访问过的结点才能算是u的孩子
			int lowv = dfs(v,u);
			lowu = min(lowu,lowv);
			if (lowv >=pre[u])
			{
				iscut[u]=1;           //u是割点
				cut[u]++;
			//	if (lowv > pre[u])       //(u,v)边时桥
			//		printf("qiao")
			}
		}
		else if (pre[v] <pre[u] && v!=fa)  //v!=fa确保了(u,v)是从u到v的反向边
		{
			lowu = min(lowu,pre[v]);
		}
	}
	if (fa < 0 && child == 1)   //若u是根且孩子数<=1,那么u就不是割点
	{
		
	}   
    else if (fa<0 && child>1)
	{
        ans.push_back(u);
	}
	else if (cut[u]>=1)
		ans.push_back(u),cut[u]++;
	return low[u]=lowu;
}
void init()
{
	dfs_clock = 0;
	sum=0;
	memset(pre,0,sizeof(pre));
	memset(iscut,0,sizeof(iscut));
	memset(cut,0,sizeof(cut));
	for (int i = 0;i<=maxn;i++)
		G[i].clear();
	ans.clear();
}
int main()
{
	int u,v;
	while (scanf("%d",&u)&& u)
	{
		init();
        while (1)
		{
			scanf("%d",&v);
			G[u].push_back(v);
			G[v].push_back(u);
			scanf("%d",&u);
			if (!u)
				break;
		}

		for (int i = 1;i<=1000;i++)
		{
			if (!pre[i] && G[i].size()>0)
			{
                dfs(i,-1);
			}
		}
		sort(&ans[0],&ans[0]+ans.size());
		
	    if (ans.size()>0)
		{
			printf("Network #%d\n",cas++);
			for (int i = 0;i<ans.size();i++)
				printf("  SPF node %d leaves %d subnets\n",ans[i],cut[ans[i]]);
		}
		else
			printf("Network #%d\n  No SPF nodes\n",cas++);
		puts("");
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值