hdu 1507 Uncle Tom's Inherited Land*(二分图最大匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507

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)

题意:给你一个n*m的地,然后给你p个点,表示这些点代表的地是不能卖的,问你最多能卖出多少块1*2的地。

找出i+j为奇数的且能卖的地,作为集合1,与这块地相邻的且能卖的地为集合2,这就转化为最大二分匹配了。

#pragma GCC optimize(2)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
#include<set>
#include<vector>
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct node
{
	int x, y;
}edge[5];
int n, m;
int linker[maxn*maxn], vis[maxn*maxn];
vector<int>g[maxn*maxn];
int mp[maxn][maxn];
set<int>s;
set<int>::iterator it;
int dfs(int x)
{
	for (int i = 0; i < g[x].size(); i++)
	{
		int v = g[x][i];
		if (!vis[v])
		{
			vis[v] = 1;
			if (linker[v] == -1 || dfs(linker[v]))
			{
				linker[v] = x;
				s.insert(v);
				return 1;
			}
		}
	}
	return 0;
}
int match()
{
	int cot = 0;
	memset(linker, -1, sizeof(linker));
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if ((i + j) % 2 == 0)
			{
				continue;
			}
			memset(vis, 0, sizeof(vis));
			cot += dfs((i - 1)*m + j);
		}
	}
	return cot;
}
bool cmp(node &a, node &b)
{
	if (a.x == b.x)
	{
		return a.y < b.y;
	}
	else
	{
		return a.x < b.x;
	}
}
int main()
{
	//freopen("C://input.txt", "r", stdin);
	while (cin >> n >> m)
	{
		if (n == 0 && m == 0)
		{
			break;
		}
		memset(mp, 0, sizeof(mp));
		s.clear();
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				mp[i][j] = 1;
			}
		}
		for (int i = 1; i <= n * m; i++)
		{
			g[i].clear();
		}
		int p;
		scanf("%d", &p);
		while (p--)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			mp[u][v] = 0;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (mp[i][j] == 1)
				{
					if ((i + j) % 2 == 1)
					{
						if (mp[i][j + 1] == 1)
						{
							g[(i - 1)*m + j].push_back((i - 1)*m + j + 1);
						}
						if (mp[i][j - 1] == 1)
						{
							g[(i - 1)*m + j].push_back((i - 1)*m + j - 1);
						}
						if (mp[i - 1][j] == 1)
						{
							g[(i - 1)*m + j].push_back((i - 2)*m + j);
						}
						if (mp[i + 1][j] == 1)
						{
							g[(i - 1)*m + j].push_back((i)*m + j);
						}
					}

				}
			}
		}
		int ans = match();
		printf("%d\n", ans);
		for (it = s.begin(); it != s.end(); it++)
		{
			int num = *it;
			edge[0].y = num % m;
			if (edge[0].y == 0)
			{
				edge[0].y += m;
			}
			edge[0].x = ((num - edge[0].y) / m + 1);
			edge[1].y = linker[num] % m;
			if (edge[1].y == 0)
			{
				edge[1].y += m;
			}
			edge[1].x = ((linker[num] - edge[1].y) / m + 1);
			sort(edge, edge + 2, cmp);
			printf("(%d,%d)--(%d,%d)\n", edge[0].x, edge[0].y, edge[1].x, edge[1].y);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Evildoer_llc/article/details/83021203