Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1247    Accepted Submission(s): 542
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). 

 
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
 
Recommend
LL
 

要输出任意一组解。

一开始时两边都是n*m-k个点做的,答案输出一半,但是错掉了,匹配数没有问题,就是输出解会出错。

后来按照奇偶分成两部分就可以了

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = ;
int uN,vN;//u,v的数目,使用前面必须赋值
int g[MAXN][MAXN];//邻接矩阵
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
for(int v = ; v < vN;v++)
if(g[u][v] && !used[v])
{
used[v] = true;
if(linker[v] == - || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = ;
memset(linker,-,sizeof(linker));
for(int u = ;u < uN;u++)
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
}
int a[][];
int b[];
int main()
{
int n,m,k;
int u,v;
while(scanf("%d%d",&n,&m)==)
{
if(n == && m == )break;
scanf("%d",&k); memset(a,,sizeof(a));
while(k--)
{
scanf("%d%d",&u,&v);
u--;v--;
a[u][v] = -;
}
int index = ;
for(int i = ;i < n;i++)
for(int j = ;j < m;j++)
if(a[i][j]!=-)
{
b[index] = i*m + j;
a[i][j] = index++;
}
uN = vN = index;
memset(g,,sizeof(g));
for(int i = ;i < n;i++)
for(int j= ;j < m;j++)
if(a[i][j]!=- && (i+j)%==)
{
u = a[i][j];
if(i > && a[i-][j]!=-)
g[u][a[i-][j]]=;
if(i < n- && a[i+][j]!=-)
g[u][a[i+][j]]=;
if(j > && a[i][j-]!=-)
g[u][a[i][j-]]=;
if(j < m- && a[i][j+]!=-)
g[u][a[i][j+]]=;
}
int ans = hungary();
printf("%d\n",ans);
for(int i = ;i <vN;i++)
if(linker[i]!=-)
{
int x1 = b[i]/m;
int y1 = b[i]%m;
int x2 = b[linker[i]]/m;
int y2 = b[linker[i]]%m;
printf("(%d,%d)--(%d,%d)\n",x1+,y1+,x2+,y2+);
}
printf("\n");
}
return ;
}

HDU 1507 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. HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输 ...

  5. HDU 1507 Uncle Tom's Inherited Land*

    题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...

  6. hdu1507 Uncle Tom's Inherited Land* 二分匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 将i+j为奇数的构成x集合中 将i+j为偶数的构成y集合中 然后就是构建二部图 关键就是构图 然 ...

  7. 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 ...

  8. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

  9. Uncle Tom's Inherited Land*

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

随机推荐

  1. 2017多校第5场 HDU 6085 Rikka with Candies bitset

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6085 题意:存在两个长度为n,m的数组A,B.有q个询问,每个询问有一个数字k,可以得到Ai%Bj=k ...

  2. 在Caffe中使用 DIGITS(Deep Learning GPU Training System)自定义Python层

    注意:包含Python层的网络只支持单个GPU训练!!!!! Caffe 使得我们有了使用Python自定义层的能力,而不是通常的C++/CUDA.这是一个非常有用的特性,但它的文档记录不足,难以正确 ...

  3. C++ Primer读书笔记

    以前阅读学习C++ Primer时的习题代码(当时代码风格格式比较渣): https://github.com/liyuan989/exercise/tree/master/c%2B%2B%20pri ...

  4. Android端与Android端利用WIFI进行FTP通信

    一.客户端通信工具类: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; im ...

  5. java 多线程剖析

    问题的缘由源自于一道简单的面试题:题目要求如下: 建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC. 解决问题前我们前补充一些基本知识: ...

  6. Mybatis学习—入门

    总结自 Mybatis官方中文文档 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手 ...

  7. Longest Valid Parentheses——仍然需要认真看看(动态规划)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. SEO如何写好文章标题

    近一半网民只看标题不点内容,许多网站有个标题和内容摘要,而这个摘要基本概括了整篇新闻的大致内容,所以的互联网信息泛滥的今天,看标题看摘要成了最快阅读新闻资讯的一种有效方式. 如何写好标题?我一直愁这事 ...

  9. 自建yum源(只演示nginx服务,其它都一样)

    (1)概述 (2)yum server端配置 1)关闭防火墙和selinux systemctl stop firewalld systemctl disable firewalld sed -ri ...

  10. Ubuntu上开启Apache Rewrite功能的方法

    Ubuntu上开启Apache Rewrite功能的方法 本文介绍ubuntn系统中开启apache的urlrewrite功能的方法. 在Windows上开启Apache的urlRewrite非常简单 ...