您的位置:首页 > 其它

Cable TV Network- POJ 1966 连通量

2015-03-25 09:44 337 查看
Cable TV Network

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 4068Accepted: 1903
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


题意:在一个无向图中,如果任意两个点之间都有路径,那么这个图就是连通的,空图,或只有一个点也都是连通的。问至少删除多少个点,可以使得这个图变成不连通,如果不能做到,就输出n。

思路:求无向图的连通量,求s到t的最小割,将点拆成i'和i'',i'-i''的流量为1,s和t的都设为INF,图中给定的边,建立双向的流量为1的边。然后求s到t的最大流,就是s到t的最小割。固定源点再枚举汇点,答案最小的那个就是最后的答案。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int u,v,flow,next;
}edge[100010];
int n,N,m,Head[110],d[110],s,t,tot,INF=1e9,link[60][60],_u[10010],_v[10010];
char str[110];
queue<int> qu;
void add(int u,int v,int f)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].flow=f;
    edge[tot].next=Head[u];
    Head[u]=tot++;
}
void init()
{
    memset(Head,-1,sizeof(Head));
    tot=0;
    int i,j,k,u,v;
    for(i=1;i<=m;i++)
    {
        u=_u[i];
        v=_v[i];
        add(u*2^1,v*2,1);
        add(v*2,u*2^1,0);
        add(v*2^1,u*2,1);
        add(u*2,v*2^1,0);
    }
    for(i=1;i<n;i++)
       if(i!=t/2)
       {
           add(i*2,i*2^1,1);
           add(i*2^1,i*2,0);
       }
    add(s*2,s*2^1,INF);
    add(s*2^1,s*2,0);
    add(t^1,t,INF);
    add(t,t^1,0);
}
int bfs()
{
    int i,j,u,v;
    memset(d,-1,sizeof(d));
    while(!qu.empty())
      qu.pop();
    qu.push(s);
    d[s]=0;
    while(!qu.empty())
    {
        u=qu.front();
        qu.pop();
        for(i=Head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;
            if(edge[i].flow>0 && d[v]==-1)
            {
                d[v]=d[u]+1;
                if(v==t)
                  return 1;
                qu.push(v);
            }
        }
    }
    return 0;
}
int dfs(int u,int f)
{
    if(u==t || f==0)
      return f;
    int ans=0,i,j,k,v;
    for(i=Head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(edge[i].flow>0 && d[v]==d[u]+1)
        {
            k=dfs(v,min(edge[i].flow,f));
            edge[i].flow-=k;
            edge[i^1].flow+=k;
            f-=k;
            ans+=k;
            if(f==0)
              break;
        }
    }
    d[u]=-1;
    return ans;
}
int dinic()
{
    tot=0;
    init();
    int ans=0;
    while(bfs())
    {
        ans+=dfs(0,INF);
        //printf("---%d\n",ans);
    }
    return ans;
}
int main()
{
    int i,j,k,u,v,minn;
    while(~scanf("%d%d",&n,&m))
    {
        memset(link,0,sizeof(link));
        for(i=1;i<=m;i++)
        {
            scanf("%s",str);
            sscanf(str,"(%d,%d)",&u,&v);
            link[u][v]=link[v][u]=1;
            _u[i]=u;_v[i]=v;
        }
        minn=INF;
        for(i=1;i<n;i++)
           if(!link[0][i])
           {
               s=0;t=i*2^1;
               k=dinic();
               //printf("%d %d\n",i,k);
               minn=min(minn,k);
           }
        if(minn==INF)
          printf("%d\n",n);
        else
          printf("%d\n",minn);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: