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. Mysql查询语句的 where子句、group by子句、having子句、order by子句、limit子句

    Mysql的各个查询语句 一.where子句   语法:select *|字段列表 from 表名 where 表达式.where子句后面往往配合MySQL运算符一起使用(做条件判断) 作用:通过限定 ...

  2. angular 2 angular quick start Could not find HammerJS

    Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...

  3. solrJ的使用--覆盖创建索引,查询,删除索引【转自http://blog.sina.com.cn/s/blog_64ac3ab10100t3mq.html】

    package com.xzhe.common.search; import java.util.ArrayList; import java.util.Collection; import java ...

  4. HtmlUnit爬取Ajax动态生成的页面内容

    HtmlUnit说白了就是一个浏览器,这个浏览器是用Java写的无界面的浏览器,正因为其没有界面,因此执行的速度还是可以滴. HtmlUnit提供了一系列的API,这些API可以干的功能比较多,如表单 ...

  5. Python读取文件行数不对

    对于一个大文件,读取每一个行然后处理,用readline()方法老是读不全,会读到一半就结束,也不报错: 总之处理的行数跟 wc -l 统计的不一样,调试了一下午,改用 with open('xxx. ...

  6. archsummit_bj2014

    http://bj2014.archsummit.com/schedule.html 大会日程 时间 2014年12月19日 会议室 二号会议厅 7:45 入场注册 8:45 开场致辞 9:30 论高 ...

  7. layer设置弹出全屏

    //弹出即全屏 var index = layer.open({ type: , content: 'http://www.layui.com', area: ['300px', '195px'], ...

  8. SQL——将两列合并成一列

    将两列合并连接成一列,需要注意的是列的格式必须是NVARCHAR或者VARCHAR类型 ), call_uuid, ) +','+agent_code '   PerDate 1 ,980408102 ...

  9. COGS 495. 窗口

    ★☆   输入文件:window.in   输出文件:window.out   简单对比时间限制:2 s   内存限制:256 MB [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左 ...

  10. UGUI世界坐标转换为UI本地坐标

    以下是实现hud跟随3D物体的脚本,只是测试用,不是开发中的代码,脚本挂在任意游戏物体上 demo下载 using UnityEngine; public class SceneFollowUI : ...