poj1896—Cable TV Network(点联通度的求解)

题目链接:传送门

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4825 Accepted: 2223

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

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.


解题思路:Merger定理,枚举源点和汇点,poj数据感觉很水呀,网上很多固定源点枚举汇点都能过


提供一组数据:4 3 (0,1) (0,2) (0,3) 很明显联通度为1


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int,int> PA;
const int N = 20900;
const int M = 109;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);

//点的联通度求解
/*
    Merger定理:当G是完全图   k(G) = |V(G)|-1
                当G不是完全图 k(G) = min(p(A,B)) 其中点A,b不相邻
                
    p(A,B):点A到B独立轨的最大条数
    
    独立轨的求解:
    1.构造一个容量网络,原图G中的每个顶点u变成两个点u,u',u和u'间连条变,容量为1
    若u,v连个点相连,则u'到v建边,v'到u建边,容量都为无穷
    2.A'为源点,B为汇点,求从A'到B的最大流
    
    求点的联通度:
    枚举所有的源点和汇点,求p(s,t)的最下值
*/

struct Edge{
    int node,c;
    Edge*next;
    Edge*redge;
}m_edge[N*2];
Edge*head[M];
int Ecnt;

void init()
{
    Ecnt = 0;
    fill( head , head+M , (Edge*)0 );
}

void mkEdge( int a , int b , int c )
{
    int t1 = Ecnt++;
    int t2 = Ecnt++;

    m_edge[t1].node = b;
    m_edge[t1].c = c;
    m_edge[t1].next = head[a];
    m_edge[t1].redge = m_edge+t2;
    head[a] = m_edge+t1;

    m_edge[t2].node = a;
    m_edge[t2].c = 0;
    m_edge[t2].next = head[b];
    m_edge[t2].redge = m_edge+t1;
    head[b] = m_edge+t2;
}

int L[M]; //层次图

bool bfs( int s , int t )
{
    fill( L , L+M , -1 );
    queue<int>q;
    q.push(s);
    L[s] = 0;

    while( !q.empty() ){
        int u = q.front();
        q.pop();

        //寻找还有残量的边
        for( Edge*p = head[u] ; p ; p = p->next ){
            if( p->c <= 0 ) continue;

            int v = p->node;
            if( -1 != L[v] ) continue;

            q.push(v);
            L[v] = L[u]+1;
        }
    }

    return -1 != L[t];
}

int dfs( int u , int e , int cf )
{
    if( u == e ) return cf;

    int tf = 0;
    for( Edge*p = head[u] ; p ; p = p->next ){
        int v = p->node;
        int c = p->c;

        if( L[u]+1 == L[v] && c > 0 && cf > tf ){
            int f = dfs( v , e , min(c,cf-tf) );
            if( 0 == f ) continue;

            p->c -= f;
            p->redge->c += f;
            tf += f;
        }
    }
    if( 0 == tf ) L[u] = -1;
    return tf;
}

int Dinic( int s , int t )
{
    int ret = 0;
    while( bfs(s,t) ){
        int ans;
        while( ans = dfs(s,t,INT_MAX ) ){
            ret += ans;
        }
    }
    return ret;
}

vector<PA>mp;

void Build( int n , int m )
{
    init();
    for( int i = 0 ; i < n ; ++i ){
        mkEdge(i,i+n,1);
    }
    for( int i = 0 ; i < m ; ++i ){
        PA s = mp[i];
        mkEdge(s.first+n,s.second,INF);
        mkEdge(s.second+n,s.first,INF);
    }
}

int main()
{
    int n,m;
    while( ~scanf("%d%d",&n,&m) ){
        while( !mp.empty() ) mp.pop_back();
        int a,b;
        for( int i = 0 ; i < m ; ++i ){
            scanf(" (%d,%d)",&a,&b);
            mp.push_back(PA(a,b));
        }
        int ans = INF;
        //枚举源点和汇点
        for( int i = 0 ; i < n ; ++i ){
            for( int j = i+1 ; j < n ; ++j ){
                Build(n,m);
                ans = min(ans,Dinic(i+n,j));
            }
        }
        if( ans == INF ) ans = n;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值