1. 程式人生 > >poj1966枚舉源匯點求點連通度

poj1966枚舉源匯點求點連通度

+= sets sent line ise order less several dir

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4851 Accepted: 2239

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

參考了網上一份EK的

http://blog.csdn.net/wangjian8006/article/details/7988221

EK的代碼量少很多啊,但是比較慢

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
const int INF=1<<29;
int mp[N][N],flow[N][N];
int pa[N];
int bfs(int n,int st,int ed){
    int a[N];
    memset(a,0,sizeof(a));
    memset(pa,-1,sizeof(pa));
    queue<int>Q;
    Q.push(st);
    a[st]=INF;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        for(int i=0;i<n;++i) {
            if(!a[i]&&mp[u][i]>flow[u][i]) {
                Q.push(i);
                pa[i]=u;
                a[i]=min(a[u],mp[u][i]-flow[u][i]);
                if(i==ed) break;
            }
        }
    }
    return a[ed];
}
int EK(int n,int sp,int fp){
    int maxflow=0,tmp;
    memset(flow,0,sizeof(flow));
    while(tmp=bfs(n,sp,fp)){
        for(int i=fp;i!=sp;i=pa[i]){
            flow[i][pa[i]]-=tmp;
            flow[pa[i]][i]+=tmp;
        }
        maxflow+=tmp;
    }
    return maxflow;
}
int main(){
    int a,b,n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;++i) mp[i][i+n]=1;
        for(int i=0;i<m;++i) {
            scanf(" (%d,%d)",&a,&b);
            mp[a+n][b]=mp[b+n][a]=INF;
        }
        int ans=INF;
        for(int i=0;i<n;++i) for(int j=i+1;j<n;++j)
        ans=min(ans,EK(2*n,i+n,j));
        if(ans==INF) printf("%d\n",n);
        else printf("%d\n",ans);
    }
}



poj1966枚舉源匯點求點連通度