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. linux===linux后台运行和关闭、查看后台任务(转)

    fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的 一.& 最经常被用到这个用在一个命令的最后,可以把这个命令放 ...

  2. C#比较两个list集合,两集合同时存在或A集合存在B集合中无

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 006 Java并发编程wait、notify、notifyAll和Condition

    原文https://www.cnblogs.com/dolphin0520/p/3920385.html#4182690 Java并发编程:线程间协作的两种方式:wait.notify.notifyA ...

  4. C基础 mariadb处理简单案例

    引言 MariaDB 是一款灰常不错开源数据库. 这里直接用它来解决业务问题. 业务需求: 现在数据库中表示按照天分表的. 突然我们需要按照月来处理数据. 例如输入一个玩家id, 查找这个玩家这个月内 ...

  5. AspxGridView在cell内显示颜色

    protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...

  6. linux下运行jmeter脚本

    1. win下生成测试计划   2. 上传至linux下 3.运行测试计划   sh jmeter.sh -n -t second_login.jmx -l res.jtl 错误1: solution ...

  7. 20:django中的安全问题

    本节主要是讲解django中的安全特性,讲述django是如何应对网站一般面临的安全性问题 跨站点脚本(XXS)攻击 跨站点脚本攻击是指一个用户把客户端脚本注入到其他用户的浏览器中.通常是通过在数据库 ...

  8. 【hdoj_1398】SquareCoins(母函数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1398 此题采用母函数的知识求解,套用母函数模板即可: http://blog.csdn.net/ten_s ...

  9. 【JBPM4】完成任务

    示例代码: ProcessEngine processEngine = Configuration.getProcessEngine(); TaskService taskService = proc ...

  10. 使用Gradle管理第三方依赖

    http://blog.bsdn.org/2012/01/02/%E4%BD%BF%E7%94%A8gradle%E7%AE%A1%E7%90%86%E7%AC%AC%E4%B8%89%E6%96%B ...