二分图最大匹配-匈牙利算法
http://blog.csdn.net/guhaiteng/article/details/52443527
//http://codeup.cn/problem.php?id=2355
模板
#include<bits/stdc++.h>
using namespace std;
const int maxn=505;//这个值要超过两边个数的较大者,因为有link
int link[maxn];
bool used[maxn];
vector<int>G[maxn];
int uN;
bool dfs(int u){
for(int i=0;i<G[u].size();i++){
if(!used[G[u][i]]){
used[G[u][i]]=true;
if(link[G[u][i]]==-1||dfs(link[G[u][i]])){
link[G[u][i]]=u;
return true;
}
}
}
return false;
}
int hungary(){
int res=0;
memset(link,-1,sizeof(link));
for(int u=0;u<uN;u++){//这里控制下标 当前为0~n-1
memset(used,false,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
void init(int n,int m){
uN=n;
for(int i=0;i<maxn;i++)G[i].clear();
for(int i=1;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
init(n,m);
printf("%d\n",hungary());
return 0;
}
二维版
http://www.codevs.cn/problem/2171/
问题拓展https://www.zhihu.com/question/27293430/answer/36199410
#include <cstdio>
#include <cstring>
using namespace std;
int X[4]={1,-1,0,0},Y[4]={0,0,1,-1};
bool used[101][101];
bool map[101][101];
int link1[101][101],link2[101][101];
int uN;
bool dfs(int x,int y){
if(x==0||y==0)return false;
for(int i=0;i<4;i++){
int newX=x+X[i],newY=y+Y[i];
if(newX>0&&newX<=uN&&newY>0&&newY<=uN&&map[newX][newY]){
if(!used[newX][newY]){
used[newX][newY]=true;
if((link1[newX][newY]==0&&link2[newX][newY]==0)||dfs(link1[newX][newY],link2[newX][newY])){
link1[newX][newY]=x;link2[newX][newY]=y;
return true;
}
}
}
}
return false;
}
int hungary(){
int res=0;
memset(link1,0,sizeof link1);
memset(link2,0,sizeof link2);
for(int i=1;i<=uN;i++){
for(int j=1;j<=uN;j++){
memset(used,0,sizeof(used));
if(map[i][j]&&dfs(i,j))res++;
}
}
return res;
}
void init(int n,int m){
uN=n;
memset(map,1,sizeof(map));
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
map[x][y]=false;
//map[y][x]=false;
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
init(n,m);
printf("%d",hungary()/2);
return 0;
}
http://codevs.cn/problem/1022/
//做法一 二分图 匈牙利算法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,k,sum,zo;
int f[51][51],map[2501][2501],march[2501],flag[2501];
int cx[5]={1,-1,0,0};
int cy[5]={0,0,1,-1};
int di(int x,int y){return (x-1)*m+y;}
int dfs(int v){
for(int i=1;i<=zo;i++){
if(map[v][i]&&!flag[i]){
flag[i]=1;
if(!march[i]||dfs(march[i])){
march[i]=v;
return 1;
}
}
}
return 0;
}
int main(){
int x,y;
cin>>n>>m>>k;
zo=n*m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
f[i][j]=1;
for(int i=1;i<=k;i++){
cin>>x>>y;
f[x][y]=0;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(f[i][j])
for(int l=0;l<4;l++)
if(f[i+cx[l]][j+cy[l]])
map[di(i,j)][di(i+cx[l],j+cy[l])]=1;
for(int i=1;i<=zo;i++){
memset(flag,0,sizeof(flag));
if(dfs(i))sum++;
}
cout<<sum/2;
return 0;
}
//做法二 染色
#include<bits/stdc++.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int n,m,k,cnt,ans=0,M[105][105],color[105*105][3];
void dfs(int CO,int x,int y){
if(M[x][y]<0)return;if(!M[x][y]){
color[cnt][M[x][y]=CO]++;
go(i,0,3){int X=x+dx[i],Y=y+dy[i];
if(X>0&&Y>0&&X<=n&&Y<=m) if(!M[X][Y])dfs(3-CO,X,Y);}}}
int main(){
int x,y;scanf("%d%d%d",&n,&m,&k);
go(i,1,k)scanf("%d%d",&x,&y),M[x][y]=-1;
go(i,1,n)go(j,1,m)if(!M[i][j])cnt++,dfs(1,i,j);
go(i,1,cnt)ans+=min(color[i][1],color[i][2]);
printf("%d\n",ans);return 0;
}//Paul_Guderian