POJ 1966 Cable TV NETWORK(网络流-最小点割集)

                                        Cable TV NETWORK
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个点,以及M个关系,让你求最少去掉多少个点才能使得其中两个点补连通;
题解:最小点割集; 没个点只能去掉一次,因此我们可将每一个点拆分为两个点中间用一条边权为1的边连接(表示只能经过一次,也就是改点只能去掉一次),然后对于其他相连得
两个点U,V: 我们将它们分别分成两个点后U1,U2,V1,V2,建立U1,V2,和V1,U2且其权值为INF(表示可以走无数次),然后根据题意,任意两点无法连通即可;这就转化为
求最小割的问题,最小割==最大流;即求任意不同且不相邻的两点间的最大流,然后取最小的即可;
参考代码:
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<string>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<stack>
  8 #include<cmath>
  9 #include<vector>
 10 #include<map>
 11 #include<set>
 12 using namespace std;
 13 #define PI acos(-1.0)
 14 #define eps 1e-8
 15 #define mem(a,b) memset(a,b,sizeof a)
 16 typedef long long LL;
 17 typedef pair<int,int> P;
 18 const int INF=0x3f3f3f3f;
 19 const int maxn=2010;
 20 int n,m,s,t,u,v,tot;
 21 struct Edge{
 22     int from,to,cap,flow;
 23     Edge(int _f,int _t,int _c,int _fl):from(_f),to(_t),cap(_c),flow(_fl) {}
 24 };
 25 vector<Edge> edges;
 26 vector<int> G[maxn];
 27 bool vis[maxn];
 28 int d[maxn],cur[maxn],flag[210][210];
 29 
 30 void Init()
 31 {
 32     mem(d,0); tot=0;
 33     for(int i=0;i<=2*n+2;i++) G[i].clear();
 34 }
 35 
 36 void Addedge(int from,int to,int cap)
 37 {
 38     edges.push_back(Edge(from,to,cap,0));
 39     edges.push_back(Edge(to,from,0,0));
 40     int m=edges.size();
 41     G[from].push_back(m-2); G[to].push_back(m-1);
 42 }
 43 
 44 bool bfs() 
 45 {
 46     memset(vis,0,sizeof vis);
 47     queue<int> q;
 48     q.push(s);
 49     d[s] = 0; vis[s] = 1;
 50     while (!q.empty()) 
 51     {
 52         int x = q.front(); q.pop();
 53         for(int i = 0; i < G[x].size(); ++i) 
 54         {
 55             Edge &e = edges[G[x][i]];
 56             if (!vis[e.to] && e.cap > e.flow) 
 57             {
 58                 vis[e.to] = 1;
 59                 d[e.to] = d[x] + 1;
 60                 q.push(e.to);
 61             }
 62         }
 63     }
 64     return vis[t];
 65 }
 66 
 67 int dfs(int x,int a) 
 68 {
 69     if(x == t || a == 0) return a;
 70     int flow = 0, f;
 71     for(int &i = cur[x]; i < G[x].size(); ++i) 
 72     {
 73         Edge &e = edges[G[x][i]];
 74         if (d[e.to] == d[x] + 1 && (f=dfs(e.to, min(a, e.cap-e.flow))) > 0) 
 75         {
 76             e.flow += f;
 77             edges[G[x][i]^1].flow -= f;
 78             flow += f; a -= f;
 79             if (a == 0) break;
 80         }
 81     }
 82     return flow;
 83 }
 84 
 85 int Maxflow(int s, int t) 
 86 {
 87     int flow = 0;
 88     while (bfs()) 
 89     {
 90         memset(cur,0,sizeof cur);
 91         flow += dfs(s, INF);
 92     }
 93     return flow;
 94 }
 95 
 96 struct Node{
 97     int u,v;
 98 } edg[maxn];
 99 
100 int build(int u,int v)
101 {
102     Init();
103     s=u+n; t=v;
104     for(int i=1;i<=n;++i) Addedge(i,i+n,1);
105     for(int i=0;i<m;i++)
106     {
107         Addedge(edg[i].u+n,edg[i].v,INF);
108         Addedge(edg[i].v+n,edg[i].u,INF);
109     }
110     return Maxflow(s,t);
111 }
112 int main()
113 {
114     while(scanf("%d%d",&n,&m)!=EOF)
115     {
116         if(!m)
117         {
118             if(n==1) puts("1");
119             else puts("0");
120             continue; 
121         }
122         mem(flag,0);
123         for(int i=0;i<m;++i)
124         {
125             scanf(" (%d,%d)", &edg[i].u, &edg[i].v);
126             edg[i].u++; edg[i].v++;
127             flag[edg[i].u][edg[i].v]=flag[edg[i].v][edg[i].u]=1;
128         }
129         int ans=INF;
130         for(int i=1;i<n;++i)
131             for(int j=i+1;j<=n;++j)
132                 if(!flag[i][j]) ans=min(ans,build(i,j));
133         ans=min(ans,n);
134         printf("%d\n",ans);
135     }
136     return 0;    
137 } 
View Code

 

  

posted @ 2018-09-14 23:09  StarHai  阅读(303)  评论(0编辑  收藏  举报