题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1516
Uncle Tom's Inherited Land Time Limit: 2 Seconds Memory Limit: 65536 KBYour 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
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N=2e4+10; int n,m,k; bool bmask[N],tag[105][105]; int head[N],sum; struct node{ int to,next; }edge[N]; void addedge(int u,int v){ //head ,next对应的是编号 edge[sum].to=v; edge[sum].next=head[u]; head[u]=sum++; } int nx,ny,cx[N],cy[N]; int find(int u){ for(int j=head[u];j>-1;j=edge[j].next){ if(!bmask[edge[j].to]){//edge[j].to&& bmask[edge[j].to]=1; if(cy[edge[j].to]==-1||find(cy[edge[j].to])){ cy[edge[j].to]=u; cx[u]=edge[j].to; return 1; } } } return 0; } int maxmatch(){ int res=0; memset(cx,-1,sizeof(cx)); memset(cy,-1,sizeof(cy)); for(int i=1;i<=nx;i++){ if(cx[i]==-1){ for(int j=1;j<=ny;j++) bmask[j]=0; res+=find(i); } } return res; } int main() { //freopen("cin.txt","r",stdin); int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; while(cin>>n>>m&&(n+m)){ scanf("%d",&k); sum=0; memset(head,-1,sizeof(head)); memset(tag,0,sizeof(tag)); memset(edge,0,sizeof(edge)); nx=ny=n*m; int a,b; for(int i=0;i<k;i++){ scanf("%d%d",&a,&b); tag[a][b]=1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(tag[i][j])continue; for(int q=0;q<4;q++){ int x=i+dir[q][0],y=j+dir[q][1]; if(x<=n&&x>0&&y<=m&&y>0&&tag[x][y]==0){ int q1=(i-1)*m+j,q2=(x-1)*m+y; //cout<<q1<<" "<<q2<<endl; addedge(q1,q2); //bmap[q1][q2]=1; } } } } int res=maxmatch(); //cout<<res<<endl; printf("%d\n",res/2); } return 0; }