/*
二分匹配
邻接表 (匈牙利算法)
*/
# include<stdio.h>
# include<algorithm>
# include<string.h>
using namespace std;
# define MAX 13000
int cnt;
int n,m,map[MAX],vis[MAX],head[MAX];
int tot;
struct node
{
// int u;
int to;
int next;
}Edge[MAX*2];
void add(int u,int v)
{
// Edge[tot].u=u;
Edge[tot].to=v;
Edge[tot].next=head[u];
head[u]=tot;
tot++;
// Edge[tot].u=v;
Edge[tot].to=u;
Edge[tot].next=head[v];
head[v]=tot;
tot++;
}
int bfs(int x)
{
for(int i=head[x]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].to;
if(!vis[v])
{
vis[v]=1;
if(!map[v]||bfs(map[v]))
{
map[v]=x;
return 1;
}
}
}
return 0;
}
int f()
{
int a=0;
memset(map,0,sizeof(map));
for(int i=0; i<=12000; i++)//总点数
{
memset(vis,0,sizeof(vis));
if(bfs(i))
a++;
}
return a;
}
int main()
{
int i,j,k,cot1=0,a,b,u,v;
while(~scanf("%d%d",&n,&m),n+m)
{
tot=0;
memset(head,-1,sizeof(head));
for(i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
add((b)*102+(a),(b)*102+a+1);///离散化
}
for(i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
add((b)*102+a,(b+1)*102+a);
}
int ans=f();
printf("%d\n",ans/2);
}
return 0;
}
hdu 4619 Warm up 2 (二分匹配)
最新推荐文章于 2019-07-24 14:35:09 发布