TZOJ 2018 SPF

描述:

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.

输入:

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. 

输出:

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.

样例输入:

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
样例输出:

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

本题是割点模板题,在使用tarjan后,将割点存储,完成后,对割点进行dfs即可求出有几个子树;

不要忘记格式问题以及他的输入输出需要使用while循环(且不能一次输出两个不然会卡住)

然后边的最大值需要使用maa记住输入中最大的那个数字

最后谨记不要忘记重置数组(

#include<bits/stdc++.h>
using namespace std;
vector<int>ma[1005];
int low[1005]={0},dfn[1005]={0},fa[1005]={0},cut[1005],vis[1005];
int ans,cnt,gen,gson,son;
void tar(int x)
{
	cnt++;
	dfn[x]=cnt;
	low[x]=cnt;
	for(int j=0;j<ma[x].size();j++)
	{
		int y=ma[x][j];
		if(dfn[y]==0)
		{
			fa[y]=x;
			tar(y);
			if(fa[x]==x)gson++;
			else
			{
				low[x]=min(low[x],low[y]);
				if(low[y]>=dfn[x])
				{
					cut[x]=1;
				}
			}
			
		}
		else
		{
			low[x]=min(low[x],dfn[y]);
		}
	}
}

void dfs(int u)
{
	vis[u]=1;
	for(int i=0;i<ma[u].size();i++)
	{
		int v=ma[u][i];
		if(vis[v]==0)
		{
			vis[v]=1;
			dfs(v);
		}
	}
}
int main()
{
	int t=0,maa=0,u,v,ff=0;
	while(cin>>u,u)
	{
		if(ff)cout<<endl;
		t++;
		for(int i=1;i<=1000;i++) ma[i].clear();
		memset(dfn,0,sizeof dfn);
		memset(fa,0,sizeof fa);
		memset(low,0,sizeof low);
		memset(cut,0,sizeof cut);
		ans=0,cnt=0,maa=0;
		cin>>v;
		ma[u].push_back(v);
		ma[v].push_back(u);
		maa=max(maa,max(u,v));
		while(cin>>u,u)
		{
			
			cin>>v;
			ma[u].push_back(v);
			ma[v].push_back(u);
			maa=max(maa,max(u,v));
		}
		gen=1,gson=0;
		fa[gen]=1;
		tar(gen);	
		if(gson>=2)
		{
			cut[gen]=1;
		}
		cout<<"Network #"<<t<<endl;
		int f=0;
		for(int i=1;i<=maa;i++)
		{
			
			if(cut[i])
			{
				f=1;
				memset(vis,0,sizeof vis);
				vis[i]=1;
				int son=0;
				for(int j=0;j<ma[i].size();j++)
				{
					v=ma[i][j];
					if(vis[v]==0)
					{
						dfs(v);
						son++;
					}
				}
				printf("  SPF node %d leaves %d subnets\n",i,son);
			}
		}
		if(!f) printf("  No SPF nodes\n");
		ff=1;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值