【连通图|点连通度】POJ-1966 Cable TV Network

来源:互联网 发布:seo网络营销是什么 编辑:程序博客网 时间:2024/05/16 10:35

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 longusing 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. 意即没有公共顶点的路径。 ↩
1 0