B. Uncle Tom's Inherited Land*

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

Special Judge
 
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) 解题:根据奇偶性建图+最大匹配数+最大匹配数的记录。根据奇偶性建图:在一个平面坐标系中,某个格子的 纵横坐标和 与其相领格子的 纵横坐标和 的绝对值相差1,即相差一个数。由于奇数与偶数相间分布,故只选择纵横坐标和为奇数或者纵横坐标和为偶数的点进行建图(从始至终只能选择其中一种方式),这样保证了是一个二分图。 这题的写法,把原先的匈牙利算法的线段的端点的由的写成了二维的,算法还是那样的。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct{
int x,y;
}link[maxn][maxn];
int n,m;
bool mp[maxn][maxn],vis[maxn][maxn];
bool isIn(int x,int y){
if(x < || x > n || y < || y > m)
return false;
return true;
}
bool dfs(int x,int y){
static const int dir[][] = {{,},{-,},{,},{,-}};
for(int i = ; i < ; i++){
int tx = x+dir[i][];
int ty = y+dir[i][];
if(isIn(tx,ty) && !vis[tx][ty] && !mp[tx][ty]){
vis[tx][ty] = true;
if(link[tx][ty].x == - || dfs(link[tx][ty].x,link[tx][ty].y)){
link[tx][ty].x = x;
link[tx][ty].y = y;
return true;
}
}
}
return false;
}
int main(){
int i,j,u,v,k;
while(scanf("%d%d",&n,&m),n+m){
scanf("%d",&k);
memset(mp,false,sizeof(mp));
while(k--){
scanf("%d%d",&u,&v);
mp[u][v] = ;
}
int ans = ;
memset(link,-,sizeof(link));
for(i = ; i <= n; i++){
for(j = ; j <= m; j++){
memset(vis,false,sizeof(vis));
if(((i+j)&) && !mp[i][j] && dfs(i,j)) ans++;
}
}
printf("%d\n",ans);
for(i = ; i <= n; i++){
for(j = ; j <= m; j++){
if(link[i][j].x != -)
printf("(%d,%d)--(%d,%d)\n",link[i][j].x,link[i][j].y,i,j);
}
}
printf("\n");
}
return ;
}

XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*的更多相关文章

  1. HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. Uncle Tom's Inherited Land*

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

  5. HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. HDU——T 1507 Uncle Tom's Inherited Land*

    http://acm.hdu.edu.cn/showproblem.php?pid=1507 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  7. XTU 二分图和网络流 练习题 J. Drainage Ditches

    J. Drainage Ditches Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Ja ...

  8. XTU 二分图和网络流 练习题 C. 方格取数(1)

    C. 方格取数(1) Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class ...

  9. ZOJ1516 Uncle Tom's Inherited Land(二分图最大匹配)

    一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个 ...

随机推荐

  1. 系统中同时存在python2和python3时 pip有时候更新后会报错 解决安装的方法如下

    官网原链接:https://pip.pypa.io/en/stable/installing/ Installation Do I need to install pip? pip is alread ...

  2. h5-16-插入SVG图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. SPRING-BOOT系列之简介

    来自:51CTO的学习视频,本博客作为一个知识点记录以及代码验证 spring boot 特点 1. 为基于spring的开发提供更快的入门体验 2. 创建可以独立运行的spring应用 3. 直接嵌 ...

  4. (026)[工具软件]剪切板管理:Ditto

    剪切板管理软件:Ditto官网:http://ditto-cp.sourceforge.net/

  5. Spark MLlib编程API入门系列之特征选择之R模型公式(RFormula)

    不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). RFormula用于将数据中的字段通过R ...

  6. Windows API函数大全四

    10. API之硬件与系统函数 ActivateKeyboardLayout 激活一个新的键盘布局.键盘布局定义了按键在一种物理性键盘上的位置与含义 Beep 用于生成简单的声音 CharToOem ...

  7. macOS 的 JDK 安装问题 (Homebrew)

    Homebrew 介绍 Homebrew 是 macOS 下的一个非常好用的包管理工具, caskroom 则是基于 Homebrew 构建的一个强大的应用程序管理器. 具体用法可以餐参考 像 Mac ...

  8. BaseAtapter

    本文用于实现一个通用的BaseAdapter类,统一产品的Adapter类,作为一个工具类,减少重复性工作,增加开发效率. 序 我们在开发项目的过程中,经常会用到ListView.GridView这一 ...

  9. spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...

  10. SQL系列学习 存储过程&事物语法

    /*学习事物基本语法*/ /*增加课室名的唯一索引*/ ALTER table class add constraint uni_ClassName unique(name) /*创建存储过程,其中增 ...