#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)
#define inf 99999999
using namespace std;
int cnt;
int source,N;
int sink,ans;
int c[101*101+10000];
int H[101*101+10000];
int hash[101*101+10000];
int d[101*101+10000];
struct egde{
int u,v,c,next;
}a[101*101*50];
void init(){
for(int i=0;i<101*101+8000;i++)
H[i]=-1;
cnt=0;
}
void add(int u,int v,int c){
a[cnt].v=v;
a[cnt].c=c;
a[cnt].next=H[u];
H[u]=cnt++;
}
void build(int u,int v,int c){
add(u,v,c);
add(v,u,0);
}
int dfs(int u,int flow){
if(u==sink)
return flow;
int tt,res=0,delta;
for(tt=H[u];~tt;tt=a[tt].next){
int &v=a[tt].v,&c=a[tt].c;
if(c&&d[u]==d[v]+1){
delta=dfs(v,min(flow,c));
c-=delta;
a[tt^1].c+=delta;
res+=delta;
flow-=delta;
if (!flow)
return res;
}
}
if(!res){
if(!--c[d[u]])
d[source]=N+1;
++c[++d[u]];
}
return res;
}
void sap(){
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
c[source]=N;
while(d[source]<N)
ans+=dfs(source,inf);
}
int main(){
int n,m,x,y,cc;
while(~scanf("%d%d",&n,&m) && (n||m)){
init();
memset(hash,0,sizeof(hash));
cc=0;
for(int i=0;i<n;i++){
scanf("%d%d",&x,&y);
if((x*101+y+1)%2==0)
build(x*101+y+1,(x+1)*101+y+1,1);
else
build((x+1)*101+y+1,x*101+y+1,1);
hash[x*101+y+1]=1;
hash[(x+1)*101+y+1]=1;
cc+=2;
}
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
if((x*101+y+1)%2==0)
build(x*101+y+1,x*101+y+1+1,1);
else
build(x*101+y+1+1,x*101+y+1,1);
hash[x*101+y+1]=1;
hash[x*101+y+1+1]=1;
cc+=2;
}
source=0;
sink=101*101+5;
N=cc+2;
ans=0;
for(int i=0;i<101*101+50;i++){
if(hash[i]){
if(i%2==0)
build(source,i,1);
else
build(i,sink,1);
}
}
sap();
printf("%d\n",ans);
}
return 0;
}
hdu 4619(sap解二分匹配)
最新推荐文章于 2020-06-17 20:06:00 发布