标签:二分匹配

4 4 6 1 1 1 4 2 2 4 1 4 2 4 4 4 3 4 4 2 3 2 2 2 3 1 0 0
4 (1,2)--(1,3) (2,1)--(3,1) (2,3)--(3,3) (2,4)--(3,4) 3 (1,1)--(2,1) (1,2)--(1,3) (2,3)--(3,3)
#include<stdio.h>
#include<string.h>
int map[5005][4],vist[5005],match[5005];
int find(int i)
{
for(int j=0;j<4;j++)
if(!vist[map[i][j]]&&map[i][j])
{
vist[map[i][j]]=1;
if(match[map[i][j]]==0||find(match[map[i][j]]))
{
match[map[i][j]]=i; return 1;
}
}
return 0;
}
int main()
{
int n,m,mp[105][105],k,a,b,ans,x[2][5005],y[2][5005];
while(scanf("%d%d",&n,&m)>0&&m+n!=0)
{
scanf("%d",&k);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
mp[i][j]=1;
while(k--)
{
scanf("%d%d",&a,&b); mp[a][b]=0;
}
int wn=0,bn=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if((i+j)%2&&mp[i][j])
{
bn++; mp[i][j]=bn; x[1][bn]=i; y[1][bn]=j;
}
else if(mp[i][j])
{
wn++; mp[i][j]=wn; x[0][wn]=i; y[0][wn]=j;
}
for(int i=1;i<=bn;i++)
for(int j=0;j<4;j++)
map[i][j]=0;
for(int i=1;i<=wn;i++)
match[i]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(mp[i][j]&&(i+j)%2)
{
if(i-1>0&&mp[i-1][j])map[mp[i][j]][2]=mp[i-1][j];
if(j-1>0&&mp[i][j-1])map[mp[i][j]][0]=mp[i][j-1];
if(j+1<=m&&mp[i][j+1])map[mp[i][j]][1]=mp[i][j+1];
if(i+1<=n&&mp[i+1][j])map[mp[i][j]][3]=mp[i+1][j];
}
ans=0;
for(int i=1;i<=bn;i++)
{
for(int j=1;j<=wn;j++)
vist[j]=0;
ans+=find(i);
}
printf("%d\n",ans);
for(int i=1;i<=wn;i++)
if(match[i])
{
int x1,y1,x2,y2;
x1=x[0][i]; y1=y[0][i];
x2=x[1][match[i]]; y2=y[1][match[i]];
if(x1>x2||x1==x2&&y1>y2)
printf("(%d,%d)--(%d,%d)\n",x2,y2,x1,y1);
else
printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2);
}
printf("\n");
}
}
hdu1507Uncle Tom's Inherited Land*(最大匹配,黑白染色),布布扣,bubuko.com
hdu1507Uncle Tom's Inherited Land*(最大匹配,黑白染色)
标签:二分匹配
原文地址:http://blog.csdn.net/u010372095/article/details/38389783