POJ 1966 Cable TV Network (无向图点连通度)

题意】给出一个由n个点,m条边组成的无向图。求最少去掉多少点才能使得图中存在两点,它们之间不连通。 【思路】回想一下s->t的最小点割,就是去掉多少个点能使得s、t不连通。那么求点连通度就枚举源点、汇点,然后取其中最小点割的最小值就好了。注意如果最大流大于节点数,则应该把它修改为节点数。 【代码】  
#include 
#include 
#include 
#include 
#include 
#include 
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 105;
const int MAXE = 5005;
const int oo = 0x3fffffff;

/* Dinic-2.0-2013.07.21: adds template.  double & int 转换方便多了,也不易出错 ~*/
template 
struct Dinic{
    struct node{
        int u, v;
        T flow;
        int opp;
        int next;
    }arc[2*MAXE];
    int vn, en, head[MAXV];
    int cur[MAXV];
    int q[MAXV];
    int path[2*MAXE], top;
    int dep[MAXV];
    void init(int n){
        vn = n;
        en = 0;
        mem(head, -1);
    }
    void insert_flow(int u, int v, T flow){
        arc[en].u = u;
        arc[en].v = v;
        arc[en].flow = flow;
        arc[en].next = head[u];
        head[u] = en ++;

        arc[en].u = v;
        arc[en].v = u;
        arc[en].flow = 0;
        arc[en].next = head[v];
        head[v] = en ++;
    }
    bool bfs(int s, int t){
        mem(dep, -1);
        int lq = 0, rq = 1;
        dep[s] = 0;
        q[lq] = s;
        while(lq < rq){
            int u = q[lq ++];
            if (u == t){
                return true;
            }
            for (int i = head[u]; i != -1; i = arc[i].next){
                int v = arc[i].v;
                if (dep[v] == -1 && arc[i].flow > 0){
                    dep[v] = dep[u] + 1;
                    q[rq ++] = v;
                }
            }
        }
        return false;
    }
    T solve(int s, int t){
        T maxflow = 0;
        while(bfs(s, t)){
            int i, j;
            for (i = 1; i <= vn; i ++)  cur[i] = head[i];
            for (i = s, top = 0;;){
                if (i == t){
                    int mink;
                    T minflow = 0x7fffffff;
                    for (int k = 0; k < top; k ++)
                        if (minflow > arc[path[k]].flow){
                            minflow = arc[path[k]].flow;
                            mink = k;
                        }
                    for (int k = 0; k < top; k ++)
                        arc[path[k]].flow -= minflow, arc[path[k]^1].flow += minflow;
                    maxflow += minflow;
                    top = mink;
                    i = arc[path[top]].u;
                }
                for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
                    int v = arc[j].v;
                    if (arc[j].flow && dep[v] == dep[i] + 1)
                        break;
                }
                if (j != -1){
                    path[top ++] = j;
                    i = arc[j].v;
                }
                else{
                    if (top == 0)   break;
                    dep[i] = -1;
                    i = arc[path[-- top]].u;
                }
            }
        }
        return maxflow;
    }
};
Dinic  dinic;

struct path{
    int u, v;
}p[MAXE];
int main(){
	//freopen("test.in", "r", stdin);
	//freopen("test.out", "w", stdout);
    int n, m;
    while(scanf("%d %d", &n, &m) != EOF){
        if (m == 0){
            if (n == 1)
                puts("1");
            else
                puts("0");
            continue;
        }
        for (int i = 0; i < m; i ++){
            scanf(" (%d,%d)", &p[i].u, &p[i].v);
            p[i].u ++, p[i].v ++;
        }
        int res = oo;
        for (int i = 1; i <= n; i ++){
            for (int j = i+1; j <= n; j ++){
                dinic.init(2*n);
                for (int k = 1; k <= n; k ++){
                        dinic.insert_flow(k, n+k, 1);
                }
                for (int k = 0; k < m; k ++){
                    dinic.insert_flow(n+p[k].u, p[k].v, oo);
                    dinic.insert_flow(n+p[k].v, p[k].u, oo);
                }
                res = min(res, dinic.solve(n+i, j));
            }
        }
        if(res == oo)   res = n;
        printf("%d
",res);
    }
	return 0;
}
  【点连通度、边连通度】 [点连通度]:最少去掉多少点才能使得图中存在两点,它们之间不连通。 [边连通度]:最少去掉多少边才能使得图中存在两点,它们之间不连通。 [有向图边连通度]:按图建立流网络,每条边容量为1,枚举源汇点求最小边割集,并取最小值。 [无向图边连通度]:把无向边转化为两条相反方向的有向边转换为有向图边连通度即可。 [点连通度]:求最小边割集变为求最小点割集,具体做法是:每个点拆成(i, i', 1)的边,原图中的边变成(u, v, oo)的边,源点s为s',汇点t还是t。然后枚举源汇点求最小点割集,并取最小值。无向图转有向图的做法和上面一样。
原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114271.html