ZOJ2182&&POJ1966-Cable TV Network(求顶点连通度)

来源:互联网 发布:java poi 导出xlsx 编辑:程序博客网 时间:2024/05/16 10:14

Cable TV Network

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


Source: Southeastern Europe 2004


题意:给出n个点和m条边,求顶点连通度


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int mp[105][2];int flag[105][105];struct Edge{    int u,v,c,next;}edge[10000];int n,m,cnt;int dist[3000];int s[3000];int que[3000],sta[3000];void addedge(int u,int v,int c){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].c=c;    edge[cnt].next=s[u];    s[u]=cnt++;    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].c=0;    edge[cnt].next=s[v];    s[v]=cnt++;}int dinic(int ss,int tt){    int ans=0;    while(true)    {        int left,right,u,v;        memset(dist,-1,sizeof(dist));        left=right=0;        que[right++]=ss;        dist[ss]=0;        while(left<right)        {            u=que[left++];            for(int i=s[u];~i;i=edge[i].next)            {                u=edge[i].u;                v=edge[i].v;                if(edge[i].c>0&&dist[v]==-1)                {                    dist[v]=dist[u]+1;                    que[right++]=v;                    if(v==tt)                    {                        left=right;                        break;                    }                }            }        }        if(dist[tt]==-1) break;        int top=0,now=ss;        while(true)        {            if(now!=tt)            {                int i;                for(i=s[now];~i;i=edge[i].next)                    if(edge[i].c>0&&dist[edge[i].v]==dist[edge[i].u]+1) break;                if(i!=-1)                {                    sta[top++]=i;                    now=edge[i].v;                }                else                {                    if(top==0) break;                    dist[edge[sta[--top]].v]=-1;                    now=edge[sta[top]].u;                }            }            else            {                int flow=INF,k;                for(int i=0; i<top; i++)                {                    if(flow>edge[sta[i]].c)                    {                        flow=edge[sta[i]].c;                        k=i;                    }                }                ans+=flow;                for(int i=0; i<top; i++)                {                    edge[sta[i]].c-=flow;                    edge[sta[i]^1].c+=flow;                }                now=edge[sta[k]].u;                top=k;            }        }    }    return ans;}void build(int x,int y,int ver,int n,int m){    cnt=0;    memset(s,-1,sizeof s);    for(int i=1; i<=n; i++)        addedge(i,i+n,1);    for(int i=0; i<m; i++)    {        addedge(mp[i][0]+n,mp[i][1],INF);        addedge(mp[i][1]+n,mp[i][0],INF);    }    addedge(x,x+n,INF);    addedge(y,y+n,INF);}int main(){    while(~scanf("%d%d",&n,&m))    {        int a,b;        int ver=n*2+1;        memset(mp,0,sizeof mp);        memset(flag,0,sizeof flag);        for(int i=0; i<m; i++)        {            scanf(" (%d,%d)",&a,&b);            a++,b++;            mp[i][0]=a,mp[i][1]=b;            flag[a][b]=1;        }        if(m==0)        {            if(n==1) printf("1\n");            else printf("0\n");            continue;        }        int ans=INF,flag1=0;        for(int i=1; i<=n; i++)        {            for(int j=i+1; j<=n; j++)            {                if(!flag[i][j])                {                    flag1=1;                    build(i,j,ver,n,m);                    ans=min(ans,dinic(i,j+n));                }            }        }        if(flag1) printf("%d\n",ans);        else printf("%d\n",n);    }    return 0;}

0 0
原创粉丝点击