poj1966 Cable TV Network

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4956 Accepted: 2292

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.


题解:

       大致的题意就是给定一个无向图,求最少去掉多少个点,可以使剩下的图不联通。如果无论删多少个点,剩下的图仍联通,则输出点数N。点数<=50。

       若无向图不连通,则图中必有两个点不连通。可以枚举这两个点S和T,求在剩下的N-2的点中最少去掉多少个点能使图不连通。在每次枚举的过程中取最小值即为答案。

       “去掉最少的的点使S,T不连通”,容易想到网络流的最大流最小割。但不同的是最小割球的是“边”,而这道题求的是“点”。所以做这道题有一个重要的技巧:拆点

       拆点,顾名思义就是把点拆开,所以我们可以按照下面的方法构建一个网络:

       1.把原来无向图的点差成x和x+n两个点。

       2.对每一个点连接一条有向边(x,x+n),容量为1.

       3.对于原无向图中的每一条边(x,y),在网络中连有向边(x+n,y),(y,x+n),容量为正无穷。

       以S为源点,T为汇点,求最小割即最大流,答案即为最少需要去掉的点数。

       将点拆成两个点,并连接一条有向边,容量为1,这样要想经过这个点就相当于必须流经这条边。所以删去一个点,就等价于断开这条边。


代码(16ms):

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <ctime>
#include <queue>
using namespace std;

const int inf=1e9+7;
const int Max=1e3+7;
int size,ans,n,m,minn,s,t;
int depth[Max],first[Max],map[55][55];
struct shu{int to,len,next;};
shu bian1[Max],bian[Max];

inline int get_int()
{
   int x=0,f=1;
   char c;
   for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
   if(c=='-') {f=-1;c=getchar();}
   for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

inline void clean()
{
   size=1;
   memset(first,0,sizeof(first));
   memset(bian1,0,sizeof(bian1));
   memset(bian,0,sizeof(bian));
   memset(map,0,sizeof(map));
}

inline void build(int x,int y,int z)
{
   size++;
   bian1[size].next=first[x];
   first[x]=size;
   bian1[size].to=y;
   bian1[size].len=z;
}

inline bool bfs()
{
   memset(depth,0,sizeof(depth));
   queue<int>q;
   q.push(s);
   depth[s]=1;
   while(!q.empty())
   {
   	 int point=q.front();
   	 q.pop();
   	 for(int u=first[point];u;u=bian[u].next)
   	 {
   	   if(!depth[bian[u].to]&&bian[u].len)
   	   {
   	   	 depth[bian[u].to]=depth[point]+1;
   	   	 q.push(bian[u].to);
   	   }
   	 }
   }
   return depth[t];
}

inline int dinic(int point,int flow)
{
   int sum=0;
   if(point==t) return flow;
   for(int u=first[point];u&&sum<flow;u=bian[u].next)
   {
   	 if(depth[bian[u].to]==depth[point]+1&&bian[u].len)
   	 {
   	   int minn=dinic(bian[u].to,min(flow-sum,bian[u].len));
   	   if(!(flow-sum)) depth[bian[u].to]=0;
   	   bian[u].len-=minn;
   	   bian[u^1].len+=minn;
   	   sum+=minn;
   	 }
   }
   return sum;
}

int main()
{
   //freopen("lx.in","r",stdin);
   //freopen("lx.out","w",stdout);

   while(scanf("%d%d",&n,&m)!=EOF)
   {
   	 if(n==1) {cout<<"1\n";continue;}
   	 if(m==0){cout<<"0\n";continue;}

     clean();
   	 for(int i=1;i<=m;i++)
	 {
	   int x=get_int(),y=get_int();
	   x++;y++;map[x][y]=map[y][x]=1;
	   build(x+n,y,inf);build(y,x+n,0);
	   build(y+n,x,inf);build(x,y+n,0);
     }
     for(int i=1;i<=n;i++)
     {
       build(i,i+n,1);
       build(i+n,i,0);
     }

     minn=inf;
     for(int i=1;i<=2;i++)
       for(int j=1;j<=n;j++)
       {
       	 if(map[i][j]||i==j) continue;
       	 for(int k=2;k<=size;k++) bian[k]=bian1[k];
       	 ans=0;s=i+n;t=j;
       	 while(bfs())
       	 {
       	   ans+=dinic(s,inf);
         }
       	 minn=min(ans,minn);
       }
     if(minn==inf) cout<<n<<"\n";
     else cout<<minn<<"\n";
   }

   return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值