求无向图的点连通度,一般的方法就是转化为网络流来求解
构建网络流模型:
若G为无向图:
(1)原G图中的每个顶点V变成N网中的两个顶点V'和V'',顶点V'至V''有一条弧容量为1;
(2)原图G中的每条边e=(U,V),在N网中有两条弧e'=(U'',V'),e''=(V'',U')与之对应,e'与e''容量均为无穷;
(3)以某点为源点,枚举汇点,求最大流。
其中源点的确立有一个条件,就是这个点不能和其他的所有点都相邻,如果都相邻,显然是无法求出最小割的。 所以在确立源点的时候不能直接随意选点。
转载了一段关于为什么只要枚举汇点:
假设点连通度为k,那么如果你枚举源汇,我们构图的时候是拆点的,所有最小割中最小的一定是k,对应k个点,割边为这k个点拆点后对应的边,那么,这个最小割把所有点点分成了两部分,S和T集合,S和T集合中的点都是不连通的,而S集合中的点都是连通的,T集合中的点也都是连通的,对于任意一个点i属于S,任意一个点j属于T,要使得他们不连通,在图中删除的点都为k,不可能更小了,否则最小割值比k小,而如果他们同时属于S集合或者T集合,使得他们不连通,要删除的点大于k,因为他们现在还连通,于是,如果我们指定一个源点,枚举汇点,如果这个源点与汇点同时在刚才那个最小割的S或T集合中,要使他们不连通,删除的点必然大于k,如果他们一个属于S,一个属于T,那么使他们不连通,要删除的点就是k个,所以,只要枚举汇点就行了
#include<iostream> #include<algorithm> #include<iomanip> #include<cstring> #include<string> #include<cstdio> #include<cmath> #include<queue> #include<map> #include<set> #define MAXN 2222 #define MAXM 222222 #define INF 1000000000 using namespace std; struct node { int ver; // vertex int cap; // capacity int flow; // current flow in this arc int next, rev; }edge[MAXM]; int dist[MAXN], numbs[MAXN], src, des, n; int head[MAXN], e; void add(int x, int y, int c) { //e记录边的总数 edge[e].ver = y; edge[e].cap = c; edge[e].flow = 0; edge[e].rev = e + 1; //反向边在edge中的下标位置 edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置 head[x] = e++; //以x为起点的边的位置 //反向边 edge[e].ver = x; edge[e].cap = 0; //反向边的初始网络流为0 edge[e].flow = 0; edge[e].rev = e - 1; edge[e].next = head[y]; head[y] = e++; } void rev_BFS() { int Q[MAXN], qhead = 0, qtail = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; numbs[i] = 0; } Q[qtail++] = des; dist[des] = 0; numbs[0] = 1; while(qhead != qtail) { int v = Q[qhead++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue; dist[edge[i].ver] = dist[v] + 1; ++numbs[dist[edge[i].ver]]; Q[qtail++] = edge[i].ver; } } } void init() { e = 0; memset(head, -1, sizeof(head)); } int maxflow() { int u, totalflow = 0; int Curhead[MAXN], revpath[MAXN]; for(int i = 1; i <= n; ++i)Curhead[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { int augflow = INF; for(int i = src; i != des; i = edge[Curhead[i]].ver) augflow = min(augflow, edge[Curhead[i]].cap); for(int i = src; i != des; i = edge[Curhead[i]].ver) { edge[Curhead[i]].cap -= augflow; edge[edge[Curhead[i]].rev].cap += augflow; edge[Curhead[i]].flow += augflow; edge[edge[Curhead[i]].rev].flow -= augflow; } totalflow += augflow; u = src; } int i; for(i = Curhead[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break; if(i != -1) // find an admissible arc, then Advance { Curhead[u] = i; revpath[edge[i].ver] = edge[i].rev; u = edge[i].ver; } else // no admissible arc, then relabel this vertex { if(0 == (--numbs[dist[u]]))break; // GAP cut, Important! Curhead[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != src) u = edge[revpath[u]].ver; // Backtrack } } return totalflow; } int main() { int m, u, v, w; int x[1555], y[1555]; int mp[55][55]; while(scanf("%d%d", &n, &m) != EOF) { memset(mp, 0, sizeof(mp)); src = n + 1; for(int i = 1; i <= m; i++) { scanf(" (%d, %d)", &u, &v); u++; v++; x[i] = u; y[i] = v; mp[u][v] = mp[v][u] = 1; } for(int i = 1; i <= n; i++) { bool flag = false; for(int j = i + 1; j <= n; j++) { if(mp[i][j] == 0) { flag = true; break; } } if(flag) { src = n + i; break; } } int N = n; n = 2 * n; int ans = INF; for(int j = 1; j <= N; j++) { if(src - N == j) continue; des = j; init(); for(int i = 1; i <= N; i++) { u = i; v = i + N; w = 1; add(u, v, w); } for(int i = 1; i <= m; i++) { u = x[i]; v = y[i]; w = INF; add(u + N, v, w); add(v + N, u, w); } rev_BFS(); ans = min(ans, maxflow()); } if(ans >= INF) ans = N; printf("%d\n", ans); } return 0; }