【二分图|最大匹配】ZOJ-1516 Uncle Tom's Inherited Land

26 篇文章 0 订阅
12 篇文章 0 订阅

Uncle Tom's Inherited Land

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

————————————————————独身の分割線————————————————————
思路:刚看到,觉得怎么会是二分图呢?经过观察不难发现,每个格子和它上下左右的格子可以匹配成一个长方形,却永远不能和它斜对角的格子匹配。
简单分析一下,发现确实是一个二分图,X部是横纵坐标之和为奇数的格子,为偶数的就是Y部。
然后我哼唧哼唧写了个哈希+前向星。。。得到了SE!估计是我哈希矬了。
并不需要哈希。只需要遍历这张图,分成X部和Y部进行编号即可。
因为建立的是单向边,所以共用编号是并不影响的。
给出两种能A的代码,邻接矩阵、前向星:
邻接矩阵代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111;
const int d[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n, m, k, cx[N], cy[N], dot, num[N][N];
bool mat[N][N], vis[N];
bool G[N][N];

void init()
{
	memset(G, 0, sizeof(G));
	for(int j = 0; j <= m+1; j++) {
		mat[0][j] = 1;
		mat[n+1][j] = 1;
	}
	for(int i = 0; i <= n+1; i++) {
		mat[i][0] = 1;
		mat[i][m+1] = 1;
	}
}

void build()
{
	for(int x = 1; x <= n; x++) {
		for(int y = 1; y <= m; y++) if((x+y)&1) {
			if(!mat[x][y]) {
				for(int dir = 0; dir < 4; dir++) {
					int nx = x + d[dir][0], ny = y + d[dir][1];
					if(!mat[nx][ny]) {
						G[num[x][y]][num[nx][ny]] = 1; 
					}
				}
			}
		}
	}
}

int dfs(int u)
{
	for(int v = 0; v < dot; v++) {
		if(G[u][v] && !vis[v]) {
			vis[v] = true;
			if(cy[v] == -1 || dfs(cy[v])) {//当前v没有匹配或者可以为其配偶找到其他匹配
				cy[v] = u;
				cx[u] = v;
				return 1;
			}
		}
	}
	return 0;
}

int match()
{
	int ret = 0;
	memset(cx, -1, sizeof(cx));
	memset(cy, -1, sizeof(cy));
	for(int i = 0; i < dot; i++) {
		if(cx[i] == -1) {
			memset(vis, 0, sizeof(vis));
			ret += dfs(i);
		}
	}
	return ret ;
}

int main()
{
#ifdef J_Sure
	freopen("000.in", "r", stdin);
	freopen("999.out", "w", stdout);
#endif
	while(scanf("%d%d", &n, &m), n||m) {
		memset(mat, 0, sizeof(mat));
		init();
		scanf("%d", &k);
		int x, y;
		for(int i = 0; i < k; i++) {
			scanf("%d%d", &x, &y);
			mat[x][y] = 1;
		}
		dot = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				if(!mat[i][j]) {
					num[i][j] = dot++;
				}
			}
		}
		build();
		int ans = match();
		printf("%d\n", ans);
	}
	return 0;
}
前向星代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111, M = 11111;
const int d[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n, m, k, tot, head[N], cou[N], num[N][N], nx, ny;
bool mat[N][N], vis[N];
struct Edge {
	int v, next;
	Edge(){}
	Edge(int _v, int _next):
		v(_v), next(_next){}
}edge[M];

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
	for(int j = 0; j <= m+1; j++) {
		mat[0][j] = 1;
		mat[n+1][j] = 1;
	}
	for(int i = 0; i <= n+1; i++) {
		mat[i][0] = 1;
		mat[i][m+1] = 1;
	}
}

void add(int u, int v)
{
	edge[tot] = Edge(v, head[u]); 
	head[u] = tot++;
}

void build()
{
	for(int x = 1; x <= n; x++) {
		for(int y = 1; y <= m; y++) if((x+y) & 1) {
			if(!mat[x][y]) {
				for(int dir = 0; dir < 4; dir++) {
					int nx = x + d[dir][0], ny = y + d[dir][1];
					if(!mat[nx][ny]) {
						add(num[x][y], num[nx][ny]);
					}
				}
			}
		}
	}
}

int dfs(int u)
{
	for(int i = head[u]; ~i; i = edge[i].next) {
		int v = edge[i].v;
		if(!vis[v]) {
			vis[v] = true;
			if(cou[v] == -1 || dfs(cou[v])) {//当前v没有匹配或者可以为其配偶找到其他匹配
				cou[v] = u;
				return 1;
			}
		}
	}
	return 0;
}

int match()
{
	int ret = 0;
	memset(cou, -1, sizeof(cou));
	for(int i = 0; i < nx; i++) {
		memset(vis, 0, sizeof(vis));
		ret += dfs(i);
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	while(scanf("%d%d", &n, &m), n||m) {
		memset(mat, 0, sizeof(mat));
		init();
		scanf("%d", &k);
		int x, y;
		for(int i = 0; i < k; i++) {
			scanf("%d%d", &x, &y);
			mat[x][y] = 1;
		}
		nx = ny = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				if(!mat[i][j]) {
					if((i+j) & 1) num[i][j] = nx++;
					else num[i][j] = ny++;
				}
			}
		}
		build();
		int ans = match();
		printf("%d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值