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. [已读]HTML5与CSS3权威指南第二版(下)

    去年下半年前公司给买的(老付对我们确实蛮好的),一人挑一本,我当时一定是秀逗了.看的时候就发现,这本书的内容过时严重,即便它是新出不久的第.二.版.其他没什么说的,总之,不推荐看.

  2. Oracle及其相关软件历史版本下载地址

    https://edelivery.oracle.com/osdc/faces/Home.jspx 打开上面这个链接,输入自己或可用的帐号即可. 搜索到自己想要下载的软件后,点击,软件会添加到购物车中 ...

  3. webfrom ASP开发基础跟模式

    ASP.NET - .net开发网站应用程序的技术总称 ASP WebForm           MVC   是ASP.NET的两个技术方法 WebForm类似于WinForm,可视化操作 MVC类 ...

  4. 外文翻译 《How we decide》被情感愚弄 第一节

    本节为第三章的起始. 书的导言 本节阅读感言:情感系统脱离控制的后果是毁灭性的. Ann Klinestiver 在一所高中做英文老师,她被诊断为患帕金森综合症.在课堂上,当她正准备和学生们谈及一些莎 ...

  5. 《Head First HTML与CSS》的HTML标签、属性

    一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...

  6. android动画之通过子线程来实现动画

    android动画之通过子线程来实现动画 使用android动画机制,往往是相对于原始位置来进行参照. 这里通过子线程修改物体位置实现动画. 布局文件: <RelativeLayout xmln ...

  7. JS正则匹配待重命名文件名

    <script>var str = "123 - Copy(2).csv";var regExp = /^123( - Copy(\(\d+\))?)?.csv$/;d ...

  8. VBA Promming——分支语句(解二元一次方程)

    分支语句 If expression1 Then expressions ElseIf expression2 Then expressions Else expression End If 注:VB ...

  9. [LUOGU] [NOIP2017] P3960 列队

    题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n \times mn×m 名学生,方阵的行 ...

  10. [LOJ] 分块九题 5

    区间开平方,区间查询. lazy标记改为区间是否全是1或者0,这样的区间是没有更新价值的. //Stay foolish,stay hungry,stay young,stay simple #inc ...