求一个图的联通度,就是找图中固定源点,然后枚举汇点求最小割。
然后最小割中最小的一个就是图的联通度。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#define MM(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
/*
ShellDawn
POJ1966
No.36
*/
#define maxn 55
int N,M;
int S,T;
struct Edge{
int to;
int v;
int rvs;
int pre;
};
Edge E[maxn*maxn*4];
int pre[maxn*2];
int cnt;
int In[maxn*maxn][2];
int V[maxn*2];
void addE(int a,int b,int v){
E[cnt].to = b;
E[cnt].v = v;
E[cnt].rvs = cnt+1;
E[cnt].pre = pre[a];
pre[a] = cnt++;
E[cnt].to = a;
E[cnt].v = 0;
E[cnt].rvs = cnt - 1;
E[cnt].pre = pre[b];
pre[b] = cnt ++;
}
bool BFS(int s){
MM(V,0);
queue<int> q;
q.push(s);
V[s] = 1;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=pre[now];i!=0;i=E[i].pre){
if(E[i].v > 0 && V[E[i].to] == 0){
V[E[i].to] = V[now] + 1;
q.push(E[i].to);
}
}
}
if(V[T] > 0) return true;
return false;
}
int DFS(int now,int minflow){
if(now == T) return minflow;
int flow = 0;
for(int i=pre[now];i!=0 && minflow; i = E[i].pre){
if(E[i].v > 0 && V[E[i].to] == V[now] + 1){
int f = DFS(E[i].to,min(minflow,E[i].v));
flow += f;
minflow -= f;
E[i].v -= f;
E[E[i].rvs].v += f;
}
}
if(flow == 0) V[now] = 0;
return flow;
}
void build(){
MM(pre,0); cnt = 1;
for(int i=0;i<N;i++) addE(i,i+N,1);
for(int i=0;i<M;i++){
int a,b;
a = In[i][0];
b = In[i][1];
addE(a+N,b,INF);
addE(b+N,a,INF);
}
}
int main(){
//freopen("A.txt","r",stdin);
while(~scanf("%d%d",&N,&M)){
if(N == 0) { printf("0\n");continue; }
if(M == 0 && N == 1) { printf("1\n");continue;}
if(M == 0 && N > 1){printf("0\n");continue;}
for(int i=0;i<M;i++) scanf(" (%d,%d)",&In[i][0],&In[i][1]);
int ans = INF;
S = N;
for(int i=1;i<N;i++){
T = i;
build();
int maxflow = 0;
while(BFS(S)) maxflow += DFS(S,INF);
ans = min(ans,maxflow);
}
printf("%d\n",ans>=INF?N:ans);
}
return 0;
}