POJ 1966 点连通度

Cable TV Network

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 2912

 

Accepted: 1348

Description

The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor f of a network with n relays is:
1. n, if the net remains connected regardless the number of relays removed from the net.
2. The minimal number of relays that disconnect the network when removed.


For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.

Input

Write a program that reads several data sets from the standard input and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two integers: 0<=n<=50,the number of relays in the net, and m, the number of cables in the net. Follow m data pairs (u,v), u < v, where u and v are relay identifiers (integers in the range 0..n-1). The pair (u,v) designates the cable that interconnects the relays u and v. The pairs may occur in any order.Except the (u,v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net.

Sample Input

0 0
1 0
3 3 (0,1) (0,2) (1,2)
2 0
5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)

Sample Output

0
1
3
0
2

Hint

The first data set encodes an empty network, the second data set corresponds to a network with a single relay, and the following three data sets encode the nets shown in figure 1.

Source

Southeastern Europe 2004

 

 

//problem_id poj 1966

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define INF 0x1f1f1f1f
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

int n,m;

int flow[105][105];
int cap[105][105];
int e[105];
int h[105];

void maxflow(int st,int ed,int src,int tar)
{
	memset(e,0,sizeof(e));
	memset(h,0,sizeof(h));

	h[src]=ed-st+1;

	for(int i=st;i<=ed;i++)
	{
		if(cap[src][i]>0)
		{
			e[i]=cap[src][i];
			e[src]-=cap[src][i];
			flow[src][i]=cap[src][i];
			flow[i][src]-=cap[src][i];
		}
	}

	while(1)
	{
		bool fl=1;
		for(int i=st;i<=ed;i++)
		{
			if(i==tar)continue;
			if(e[i]>0)
			{
				fl=0;
				bool push=0;
				for(int j=st;j<=ed;j++)
				{
					if(cap[i][j]>flow[i][j]&&h[i]==h[j]+1)
					{
						push=1;
						int d=MIN(cap[i][j]-flow[i][j],e[i]);
						flow[i][j]+=d;
						flow[j][i]-=d;
						e[i]-=d;
						e[j]+=d;
						if(e[i]==0)break;
					}
				}
				if(!push)
				{
					int min=INF;
					for(int j=st;j<=ed;j++)
					{
						if(cap[i][j]>flow[i][j]&&h[j]<min)
						{
							min=h[j];
						}
					}
					h[i]=min+1;
				}
				break;
			}
		}
		if(fl)break;
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(cap,0,sizeof(cap));
		for(int i=0;i<m;i++)
		{
			int a,b;
			scanf(" (%d,%d)",&a,&b);
			cap[a+n][b]=cap[b+n][a]=INF;
		}
		for(int i=0;i<n;i++)
		{
			cap[i][i+n]=1;
		}
		int r=INF;
		//for(int i=0;i<n;i++)
		{
			for(int j=1;j<n;j++)
			{
				//if(i==j)continue;
				memset(flow,0,sizeof(flow));
				maxflow(0,2*n-1,n,j);
				if(e[j]<r)
				{
					r=e[j];
				}
			}
		}
		if(r>=INF)
		{
			printf("%d\n",n);
		}
		else
		{
			printf("%d\n",r);
		}
	}
	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值