杭电HDU ACM Uncle Tom's Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2496    Accepted Submission(s): 1028
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).
杭电HDU ACM Uncle Tom's Inherited Land*(二分图匹配 建模)_第1张图片
 

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)
 

Source
South America 2002 - Practice
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1281  1068  1528  1151  1150
题目意思:
给定一个  n * m 的矩阵格子, 第二行给定一个k值表示池塘,接下来给出池塘坐标,要求的就是在不是池塘的区域用1 * 2 的小矩形填充它 ,问 最多能填充几个小矩形。

这个是昨天左右让我看的一个题目,说他怎么也找不到他的bug ,先来说说我的代码,将二分图所有点按照 各点横纵坐标加和的奇偶性来进行划分。因为如果一个小格子为奇数,那么他周围相邻格子必然为偶数。也就是说只有相邻的具有奇偶两者的格子才构成了1 * 2小矩形,那么此时就符合了二分图意义,实质上就是用二分图最大匹配求最多有多少个奇偶连边,这些边保证了不会交叉。

为了方便建图,将所有不是陆地部分从1……num……开始标记,然后枚举任意一个非池塘点,搜索其周围,用G数组保存二分图,那么注意G数组大小最大可能为n * m,或着直接用num - 1的值,也就是陆地小格子的个数。对于如何输出匹配边,用一个二维数组或者一个结构体pre【】【】表示dic数组中某点值的横纵坐标,那么对于所有的偶数格子如果有匹配的前提下,即from【i】!=  -1,而等于i 点的右匹配点的值,然后通过pre数组查询其横纵坐标即可。

详情看代码:
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-27 10:36
#
#     Filename: E.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/

#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
using namespace std;
const int INF=201; 
int cnt[INF][INF];
int dic[INF][INF];
int from[INF],tot;
int pre[INF][2];
bool use[INF];
int dir[][2] = {{1,0},{0,1},{0,-1},{-1,0}};
vectorG[INF * INF];
    int n, m;
    int num ;

bool match(int x)
{
     for(int i=0;i= 1 && x <= n && y >=1 && y <=m && dic[x][y])
                            G[dic[i][j]].push_back(dic[x][y]);
                    }

            }
        }
        int ans = hungary();
        cout<


你可能感兴趣的:(二分匹配)