【連通圖|點連通度】POJ-1966 Cable TV Network

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K

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.

Source
Southeastern Europe 2004


題意:給出一個無向圖,求該圖的點連通度。
思路:求點連通度的問題要轉化成網絡最大流問題。
首先拆點,比如將u點拆成u’和u”點,然後建立u’->u”的一條容量爲1的邊。再將原來的u->v的邊添加到u”->v’上,容量設置成INF。但是由於該題是無向圖,所以v”->u’也要添加容量爲INF的邊。
求一張圖的點連通度,要求出任意兩點之間的最小的獨立軌1個數。這是因爲:
P(A, B)表示不相鄰的兩點之間的獨立軌的個數,那麼顯然去掉至少P(A, B)個頂點可以使得圖不連通。
定理:(設k爲點連通度)

因此,可以固定一個源點,枚舉每個匯點求出最大流。這個最大流就是P(A, B)。
代碼如下:

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 100 + 5, M = 100*99 + 5;
int n, m, tot;
int src, sink;
int cur[N], head[N], lev[N];
int s[N];

struct Edge {
    int u, v, w, next;
    Edge(){}
    Edge(int _u, int _v, int _w, int _next):
        u(_u), v(_v), w(_w), next(_next){}
}e[M], ef[M];

void add(int u, int v, int w)
{
    ef[tot] = Edge(u, v, w, head[u]);
    head[u] = tot++;
    ef[tot] = Edge(v, u, 0, head[v]);
    head[v] = tot++;
}

void init()
{
    memset(head, -1, sizeof(head));
    tot = 0;
    for(int i = 0; i < n; i++) {
        add(i, i+n, 1);
    }
}

bool bfs()
{
    queue <int> q;
    memset(lev, -1, sizeof(lev));
    lev[src] = 0;
    q.push(src);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(int i = head[u]; ~i; i = e[i].next) {
            int v = e[i].v;
            if(e[i].w && lev[v] == -1) {
                lev[v] = lev[u] + 1;
                q.push(v);
                if(v == sink) return true;
            }
        }
    }
    return false;
}

int Dinic()
{
    int ret = 0;
    while(bfs()) {
        memcpy(cur, head, sizeof(cur));
        int u = src, top = 0;
        while(1) {
            if(u == sink) {
                int mini = INF, loc;
                for(int i = 0; i < top; i++) {
                    if(mini > e[s[i]].w) {
                        mini = e[s[i]].w;
                        loc = i;
                    }
                }
                for(int i = 0; i < top; i++) {
                    e[s[i]].w -= mini;
                    e[s[i]^1].w += mini;
                }
                ret += mini;
                top = loc;
                u = e[s[top]].u;
            }
            int &i = cur[u];
            for(; ~i; i = e[i].next) {
                int v = e[i].v;
                if(e[i].w && lev[v] == lev[u] + 1) break;
            }
            if(~i) {
                s[top++] = i;
                u = e[i].v;
            }
            else {
                if(!top) break;
                lev[u] = -1;
                u = e[s[--top]].u;
            }
        }
    }
    return ret;
}

void copy()
{
    for(int i = 0; i < tot; i++) {
        e[i] = ef[i];
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(~scanf("%d%d", &n, &m)) {
        int u, v;
        init();
        for(int i = 0; i < m; i++) {
            scanf(" (%d,%d)", &u, &v);
            add(u+n, v, INF);
            add(v+n, u, INF);
        }
        int ans = INF;
        src = n;
        for(int i = 1; i < n; i++) {
            sink = i;
            copy();
            ans = min(ans, Dinic());
        }
        if(ans == INF) ans = n;
        printf("%d\n", ans);
    }
    return 0;
}

  1. 意即沒有公共頂點的路徑。
發佈了437 篇原創文章 · 獲贊 20 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章