码迷,mamicode.com
首页 > Web开发 > 详细

POJ1966 Cable TV Network

时间:2019-06-11 10:54:22      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:存在   多少   rest   mes   key   拆点   tps   lin   不可   

Cable TV Network

Language:
Cable TV Network
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 5273Accepted: 2446

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

给你一个无向图,这个图有一个安全系数f,f的定义是:

  1. f为n,如果不管删除多少个顶点,剩下的图仍然是连通的
  2. f为删除最少的顶点数,使得剩下的图不连通

给你一个图,求出f

题解

参照ihge2k的题解。

图的连通度分为点连通度和边连通度:

  1. 点连通度:只许删点,求至少要删掉几个点(当然,s和t不能删去,这里保证原图中至少有三个点);
  2. 边连通度:只许删边,求至少要删掉几条边。

并且,有向图和无向图的连通度求法不同,因此还要分开考虑(对于混合图,只需将其中所有的无向边按照
无向图的办法处理、有向边按照有向图的办法处理即可)。

  1. 有向图的边连通度:
    这个其实就是最小割问题。以s为源点,t为汇点建立网络,原图中的每条边在网络中仍存在,容量为1,求该网络的最小割(也就是最大流)的值即为原图的边连通度。
  2. 有向图的点连通度:
    需要拆点。建立一个网络,原图中的每个点i在网络中拆成i‘与i‘‘,有一条边<i‘, i‘‘>,容量为1 (<s‘, s‘‘>和<t‘, t‘‘>例外,容量为正无穷)。原图中的每条边<i, j>在网络中为边<i‘‘, j‘>,
    容量为正无穷。以s‘为源点、t‘‘为汇点求最大流,最大流的值即为原图的点连通度。
    说明:最大流对应的是最小割。显然,容量为正无穷的边不可能通过最小割,也就是原图中的边和s、t两个点不能删去;若边<i, i‘‘>通过最小割,则表示将原图中的点i删去。
  3. 无向图的边连通度:
    将图中的每条边(i, j)拆成<i, j>和<j, i>两条边,再按照有向图的办法(【1】)处理;
  4. 无向图的点连通度:
    将图中的每条边(i, j)拆成<i, j>和<j, i>两条边,再按照有向图的办法(【2】)处理。

于是对于本题我们可以枚举源点和汇点求解,时间复杂度\(o(n^6)\)scanf的要求挺严格的,空格都要管。

#include<iostream>
#include<cstring>
#include<queue>
#define il inline
#define co const
using namespace std;

co int N=51,M=2e4;
int n,m,s,t;
int a[M],b[M],d[N*2];
int head[N*2],edge[M],leng[M],next[M],tot;
il void add(int x,int y,int z){
    edge[++tot]=y,leng[tot]=z,next[tot]=head[x],head[x]=tot;
    edge[++tot]=x,leng[tot]=0,next[tot]=head[y],head[y]=tot;
}
bool bfs(){
    memset(d,0,sizeof d),d[s]=1;
    queue<int> q;q.push(s);
    while(q.size()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=next[i]){
            int y=edge[i],z=leng[i];
            if(z&&!d[y]) d[y]=d[x]+1,q.push(y);
        }
    }
    return d[t]!=0;
}
int dfs(int x,int capc){
    if(x==t) return capc;
    int rest=capc;
    for(int i=head[x];i&&rest;i=next[i]){
        int y=edge[i],z=leng[i];
        if(z&&d[y]==d[x]+1){
            int delta=dfs(y,min(rest,z));
            if(!delta) d[y]=0;
            leng[i]-=delta,leng[i^1]+=delta;
            rest-=delta;
        }
    }
    return capc-rest;
}
void Cable_TV_Network(){
    for(int i=0;i<m;++i) scanf(" (%d,%d)",a+i,b+i)/*,fprintf(stderr,"in %d %d\n",a[i],b[i])*/;
    int ans=1e9;
    for(s=0;s<n;++s)for(t=0;t<s;++t)if(s!=t){
        memset(head,0,sizeof head),tot=1;
        for(int i=0;i<n;++i)
            i==s||i==t?add(i,i+n,1e9):add(i,i+n,1);
        for(int i=0;i<m;++i)
            add(a[i]+n,b[i],1e9),add(b[i]+n,a[i],1e9);
        int maxflow=0;
        while(bfs())
            for(int delta;delta=dfs(s,1e9);) maxflow+=delta;
        ans=min(ans,maxflow);
    }
    if(n<=1||ans==1e9) ans=n;
    printf("%d\n",ans);
}
int main(){
    while(~scanf("%d%d",&n,&m)) Cable_TV_Network();
    return 0;
}

POJ1966 Cable TV Network

标签:存在   多少   rest   mes   key   拆点   tps   lin   不可   

原文地址:https://www.cnblogs.com/autoint/p/11002286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!