点连通度和边连通度问题都可以转化为最小割最大流问题。用拆点法,相同点连接一条边,容量为1,不同点连2条边i+1→j,j+1→i,容量为INF。
注意scanf的用法,还可以这么用,以前不知道。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<sstream>
#define LL long long
#define OJ_PRINT 0
#define READ_FILE 0
using namespace std;
const int NN_MAX = 510;
const int MM_MAX = NN_MAX*NN_MAX;
const int INF = 0x0fffffff;
struct Edge{
int from,to,cap,flow;
Edge (int a1,int a2,int a3,int a4):from (a1),to (a2),cap (a3),flow (a4){}
Edge (){}
};
/**********************************************************/
int n,m,s,t;
int a[NN_MAX],p[NN_MAX],cap[NN_MAX][NN_MAX],flow[NN_MAX][NN_MAX];
int maxFlow=0;
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void EK ();
/**********************************************************/
int main()
{
if (READ_FILE) freopen ("in.txt","r",stdin);
while (scanf ("%d %d",&n,&m)!=EOF)
{
char tmp[3];
int a1,a2;
memset (cap,0,sizeof (cap));
for (int i=0;i<m;i++)
cap[i*2][i*2+1]=1;
for (int i=0;i<m;i++){
//getchar ();
scanf (" (%d,%d)",&a1,&a2);
//printf ("%c%d%c%d%c\n",tmp[0],a1,tmp[1],a2,tmp[2]);
cap[a1*2+1][a2*2]=cap[a2*2+1][a1*2]=INF;
}
s=1;
int deg=INF;
maxFlow=0;
for (t=2;t<n*2;t+=2){
maxFlow=0;
EK ();
deg=min_2 (deg,maxFlow);
if (OJ_PRINT)
if (maxFlow<=deg)
printf ("%d %d-%d\n",s,t,maxFlow);
}
if (deg==INF)
printf ("%d\n",n);
else printf ("%d\n",maxFlow);
}
return 0;
}
void EK ()
{
memset (flow,0,sizeof (flow));
while (1)
{
memset (a,0,sizeof (a)); a[s]=INF;
queue<int> qee; qee.push (s);
while (!qee.empty ())
{
int x=qee.front ();qee.pop ();
for (int y=0;y<n*2;y++){
if (!a[y] && cap[x][y]>flow[x][y]){
p[y]=x;
a[y]=min_2 (a[x],cap[x][y]-flow[x][y]);
qee.push (y);
}
}
}
if (a[t]==0) break;
maxFlow+=a[t];
for (int i = t; i != s; i=p[i]){
flow[p[i]][i]+=a[t];
flow[i][p[i]]-=a[t];
}
}
}