现在的位置: 首页 > 综合 > 正文

HDU 1507 Uncle Tom’s Inherited Land* 二分图的最大匹配

2018年05月02日 ⁄ 综合 ⁄ 共 3188字 ⁄ 字号 评论关闭

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2035    Accepted Submission(s): 842
Special Judge

Problem Description
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 first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity.
If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.
 

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 (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)
/*
HDU 1507 二分图的最大匹配
二分匹配的题目,提取信息然后找到可以匹配的两个集合,
这两个集合需要满足的条件是,集合自身元素没有关联性,

将棋盘中i+j为奇数的做A集合,偶数的做B集合,
相邻的则建立联系。于是便转换成寻找最大匹配的问题,
一个点只能与之上下左右的点相连,所以只需考虑这四个方向就可以了。
*/

#include<iostream>
#include<stdio.h>
using namespace std; 
#define N 105
#define MAX 10010
int map[N][N];
int G[MAX][4];//G[k][]表示和k=i*M+j,相连的可填充点,邻接表建立AB集合之间的联系 
int vis[MAX],match[MAX];
int n,m,k,v,ans;
void build()
{
	int  i,j,a,u;
	memset(G,-1,sizeof(G));
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			u=i*m+j;
			a=0;
			if(!map[i][j]&&((i+j)&1)==1)//可填充,奇 
			{
				//左 
	            if(j>0&&!map[i][j-1]) 
	            { 
	                G[u][a++]=u-1; 
	            } 
	            //右 
	            if(j<m-1&&!map[i][j+1]) 
	            { 
	                G[u][a++]=u+1; 
	            } 
	            //上 
	            if(i>0&&!map[i-1][j]) 
	            { 
	                G[u][a++]=u-m; 
	            } 
	            //下 
	            if(i<n-1&&!map[i+1][j]) 
	            { 
	                G[u][a++]=u+m; 
	            } 
			}
		}
}

int dfs(int u)
{
	int i,t;
	for(i=0;G[u][i]!=-1;i++)
	{
		t=G[u][i];
		if(!vis[t]&&G[u][i]!=-1)
		{
			vis[t]=1;
			
			if(match[t]==-1||dfs(match[t]))
			{
				match[t]=u;
				return 1;
			}
		}
	}
	return 0;
}

int maxMatch()
{
	int i;
	ans=0;
	 
	memset(match,-1,sizeof(match));
	for(i=0;i<v;i++)
	{
		memset(vis,0,sizeof(vis));
		if(dfs(i))
			ans++;
	}
	return ans;
}
int main()
{
	int i,j,a,b,u;
	
//	freopen("test.txt","r",stdin);
	while(scanf("%d%d",&n,&m)&&(n+m))
	{
		scanf("%d",&k);
		memset(map,0,sizeof(map));

		for(i=0;i<k;i++)
		{
			scanf("%d%d",&a,&b);
			map[a-1][b-1]=1;
		}
		v=n*m;//总和
		 
		build();
		maxMatch();
		printf("%d\n",ans);
		for(i=0;i<v;i++)
		{
			if(match[i]!=-1)
			{
				u=match[i];
				printf("(%d,%d)--(%d,%d)\n",u/m+1,u%m+1,i/m+1,i%m+1);
			}
		}
		printf("\n");	
	}
	return 0;
}

输出结果不是和题目完全一致的,但是也过了。

抱歉!评论已关闭.