Cable TV Network-POJ1966图的连通度
Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4404 Accepted: 2047
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:
- n, if the net remains connected regardless the number of relays removed from the net.
- 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
本题求的是图的连通度,应用的是Menger定理,采用计算两点之间独立轨的数目,来计算图的连通度。
/*求图的连通度,应用Menger定理,
*即无向图G的顶点连通度K(G)与顶点间的最大独立轨数目之间的关系
*if G是完全图
K{G}=|V(G)|-1
*else
K(G)=min(P{A,B}) A,B不相邻
*
*/
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
const int Max = 110;
const int INF = 0x3f3f3f3f;
int Map[Max][Max];
int MaxFlow(int n,int s,int t)
{
int Flow[Max][Max]; //记录当前网络的流。
int MinFlow[Max];//记录路径上的最小值
int pre[Max];//记录父节点
queue<int>Q;
int ans = 0;
memset(Flow,0,sizeof(Flow));
while(1)
{
while(!Q.empty())
{
Q.pop();
}
Q.push(s);
memset(pre,-1,sizeof(pre));
pre[s] = -2;
MinFlow[s]=INF;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=0;i<n;i++)
{
if(pre[i]==-1&&Flow[u][i]<Map[u][i])
{
pre[i]=u;
Q.push(i);
MinFlow [i] = min(MinFlow[u],(Map[u][i]-Flow[u][i]));
}
}
if(pre[t]!=-1)//判断是否存在增广轨。
{
int k = t;
while(pre[k]>=0)
{
Flow[pre[k]][k]+=MinFlow[t];
Flow[k][pre[k]]-=MinFlow[t];
k = pre[k];
}
break;
}
}
if(pre[t]==-1)
{
return ans;
}
else
{
ans += MinFlow[t];
}
}
}
int n,m;
int main()
{
while(~scanf("%d %d:",&n,&m))
{
int u,v;
memset(Map,0,sizeof(Map));
for(int i=0;i<n;i++)
{
Map[i][i+n]=1;
}
for(int i=1;i<=m;i++)
{
scanf(" (%d,%d)",&u,&v);
Map[u+n][v]=Map[v+n][u]=INF;
}
int ans = INF;
for(int i=1;i<n;i++)//枚举找最小的独立轨的数目
{
ans = min(ans,MaxFlow(n*2,n,i));
}
if(ans == INF)
{
ans = n;
}
printf("%d\n",ans);
}
return 0;
}