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.
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
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 180 #define edge 10080 #define inf 0x3f3f3f3f int first[maxn],dis[maxn],num[maxn]; int ww[edge],nxt[edge],vv[edge]; int e,NN,n,m; struct Edge { int u, v; }cur[edge]; inline int min(int a,int b) { return a>b?b:a; } void addEdge(int u,int v,int w) { vv[e] = v; ww[e] = w; nxt[e] = first[u]; first[u] = e++; vv[e] = u; ww[e] = 0; nxt[e] = first[v]; first[v] = e++; } int dfs(int u,int s,int d,int cost) { if(u == d) return cost; int _min = NN; int ans = 0; for(int i = first[u];i != - 1;i = nxt[i]) { int v = vv[i]; if(ww[i]) { if(dis[v] + 1 == dis[u]) { int t = dfs(v,s,d,min(ww[i],cost)); ww[i] -= t; ww[i^1] += t; cost -= t; ans += t; if(dis[s] == NN) return ans; if(!cost) break; } if(_min > dis[v]) _min = dis[v]; } } if(!ans) { if(--num[dis[u]] == 0) dis[s] = NN; dis[u] = _min + 1; ++num[dis[u]]; } return ans; } int isap(int s,int d) { memset(dis,0,sizeof(dis)); memset(num,0,sizeof(num)); num[0] = NN; int ans = 0; while(dis[s] < NN) ans += dfs(s,s,d,inf); return ans; } void build() { memset(first,-1,sizeof(first)); e = 0; for(int i = 1;i <= m;i++) { addEdge(2*cur[i].u,2*cur[i].v-1,inf); addEdge(2*cur[i].v,2*cur[i].u-1,inf); } for(int i = 1;i <= n;i++) { addEdge(2*i-1,2*i,1); } } int main() { //freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)==2) { if(m == 0) { if(n > 1)printf("%d\n",0); else printf("%d\n",n); continue; } memset(first,-1,sizeof(first)); NN = 2*n; for(int i = 1;i <= m;i++) { while(getchar()!='('); scanf("%d",&cur[i].u); getchar(); scanf("%d",&cur[i].v); getchar(); cur[i].u++;cur[i].v++; } int ans = inf; for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(i == j) continue; build(); ans = min(ans,isap(2*i,2*j-1)); } } if(ans == inf) ans = n; printf("%d\n",ans); } return 0; }