HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3339 Accepted Submission(s): 1394
Special Judge
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).
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.
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.
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
(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)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int read() {int x=,f=;char c=getchar();while(c<''||c>'') {if(c=='-')f=-;c=getchar();}while(c>=''&&c<='') {x=x*+c-'';c=getchar();}return x*f;}
int n,m,cnt;
int dis[][]={{,},{,-},{,},{-,}}; int tot,head[N];
int w[][],t[N],x[N],y[N];
struct man
{
int x,y,color,num;
};
struct EDG
{
int to,next;
}edg[M];
struct ANS
{
int x,y;
}answ[N];
void add(int u,int v)
{
edg[cnt].to=v;edg[cnt].next=head[u];head[u]=cnt++;
}
void bfs(int u,int v)
{
queue<man>q;
man s;s.color=;s.num=++tot;s.x=u;s.y=v;q.push(s);
w[u][v]=;answ[tot].x=u;answ[tot].y=v;
while(!q.empty()){
man t=q.front();q.pop();
for(int i=;i<;i++){
int xx=t.x+dis[i][];int yy=t.y+dis[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&!w[xx][yy]){
man k;k.color=(t.color+)%;k.num=++tot;k.x=xx;k.y=yy;
q.push(k);w[xx][yy]=;answ[k.num].x=xx;answ[k.num].y=yy;
if(!t.color)add(t.num,k.num);
else add(k.num,t.num);
}
}
}
}
bool dfs(int u) {
for(int i=head[u];i!=-;i=edg[i].next) {
int v=edg[i].to;
if(!t[v]) {
t[v]=;
if(!y[v]||dfs(y[v])) {
x[u]=v;
y[v]=u;
return true;
}
}
}
return false;
}
void MaxMatch() {
int ans=;
for(int i=; i<=tot; i++) {
if(!x[i]) {
met(t,);
if(dfs(i))ans++;
}
}
printf("%d\n",ans);
for(int i=;i<=tot;i++){
if(x[i]){
int v=x[i];
printf("(%d,%d)--(%d,%d)\n",answ[i].x,answ[i].y,answ[v].x,answ[v].y);
}
}
printf("\n");
}
int main() {
while (~scanf("%d%d",&n,&m)&&n&&m) {
met(w,);met(head,-);met(x,);met(y,);met(edg,);met(answ,);cnt=;tot=;
int k=read();
while(k--){
int x=read();int y=read();
w[x][y]=;
}
for(int i=;i<=n;i++)for(int j=;j<=m;j++)if(!w[i][j])bfs(i,j);
MaxMatch();
}
return ;
}
HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)的更多相关文章
- 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 ...
- 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 ...
- HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色
原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...
- HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输 ...
- HDU 1507 Uncle Tom's Inherited Land*
题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...
- 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 ...
- 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 ...
- hdu1507 Uncle Tom's Inherited Land* 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 将i+j为奇数的构成x集合中 将i+j为偶数的构成y集合中 然后就是构建二部图 关键就是构图 然 ...
- XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*
B. Uncle Tom's Inherited Land* Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I ...
随机推荐
- Linux的三种特殊权限
1.Suid Set位权限 ●对文件以文件的拥有者身份执行文件 ●对目录无影响 权限设置: ●suid=u+s 2.Sgid Set位权限 ●对文件以文件的组身份执行文件 ●对目录在目录中最新创建的文 ...
- shell指令expr和test指令
通过expr指令可以进行+.-.*.\.%等运算,但是有一点值得注意,使用乘法时,要在*前加上一个\符号. 通过test指令可以进行逻辑测试,进行测试的情况有四种: 1.整数测试 a.判断两个整数是否 ...
- SharePoint 2013 开发——开发自定义操作APP
博客地址:http://blog.csdn.net/FoxDave 自定义操作即我们所说的Ribbon和ECB(Edit Control Block),在SharePoint 2013之前,我们可以 ...
- innerHTML..innerText..textContent
/* * innerText和textContent 都是设置文字内容的,如果设置的内容当中有标签,也会直接的以文本的方式显示(标签的<>都会按照转义的方式进行解析) * innerTex ...
- 2013年7月份第3周51Aspx源码发布详情
批量重命名文件工具源码 2013-7-19 [VS2010]功能介绍:这是一个新型的文件重命名,主要用了TreeView(树形视图)来选择文件夹,批量进行文件重命名.其中,有"编号在前,编 ...
- poj1845 数论
//Accepted 204K 16MS //约数和 //n=p1^e1*p2^e2***pk^ek //约数和为:(p1^0+p1^1+..+p1^e1)*(p2^0+p2^1+..+p2^e2)* ...
- MVP架构。。。。
Model-View-Presenter(MVP)概述 MVC模式已经出现了几十年了,在GUI领域已经得到了广泛的应用,由于微软ASP.NET MVC Framework的出现,致使MVC一度成 ...
- The ShortCuts in the ADT (to be continued)
1. automatically add all the namespace which need to be include in the class. ctrl+shift+o
- C++面向对象编程解决三阶矩阵相加减
/*此处用面向对象编程*/ #include<iostream> #include<string.h> using namespace std; class Matrices ...
- iPhone各控件的默认高度
1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图 用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半透明 ...