Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)
Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle's property. Furthermore, ponds are not salable property.
Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).
Input
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.
Output
For each test case in the input your program should produce one line of output, containing an integer value representing the maximum number of properties which can be sold.
Sample Input
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
Sample Output
4
3
Source: South America 2002, Practice
题意:有一个n*m的地图,告诉你这上面池塘的坐标,将剩余的土地每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地
解题思路:二分图,将相邻的两块地一块放在X集合,一块放在Y集合
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int n,m;
int xn,yn;
int x[5009],y[5009];
int s[5009],nt[20050],e[20050];
int visit[5009];
int mp[105][105];
struct node
{
int x,y;
}sx[5009],sy[5009];
bool path(int k)
{
for(int i=s[k]; ~i; i=nt[i])
{
int ee=e[i];
if(ee==0) continue;
if(!visit[ee])
{
visit[ee]=1;
if(!y[ee]||path(y[ee]))
{
y[ee]=k;
x[k]=ee;
return 1;
}
}
}
return 0;
}
void MaxMatch()
{
int ans=0;
memset(x,0,sizeof x);
memset(y,0,sizeof y);
for(int i=1; i<=xn; i++)
{
if(!x[i])
{
memset(visit,0,sizeof visit);
if(path(i)) ans++;
}
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d %d",&n,&m)&&(n+m))
{
int cnt=1,k,a,b;
scanf("%d",&k);
memset(s,-1,sizeof s);
memset(nt,-1,sizeof nt);
memset(mp,0,sizeof mp);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&a,&b);
mp[a][b]=1;
}
xn=1,yn=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(!mp[i][j])
{
if((i+j)&1) sx[xn].x=i,sx[xn++].y=j;
else sy[yn].x=i,sy[yn++].y=j;
}
}
}
for(int i=1;i<xn;i++)
{
for(int j=1;j<yn;j++)
{
if((sx[i].x==sy[j].x&&abs(sx[i].y-sy[j].y)==1)||(sx[i].y==sy[j].y&&abs(sx[i].x-sy[j].x)==1))
nt[cnt]=s[i],s[i]=cnt,e[cnt++]=j;
}
}
MaxMatch();
}
return 0;
}