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 ...
随机推荐
- 蓝桥杯 algo——6 安慰奶牛 (最小生成树)
问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计 划除去P条道路中尽可能多的道路 ...
- HTML---6 运算符,类型转换
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException
1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override publi ...
- python3 nonlocal vs global
考虑这样一个python程序: x = 12 def func(): x = 1 func() print(x) 输出为:x = 12 因为函数内部定义的x被认为只属于局部作用域,为了表明我么引用的是 ...
- 解决:Ubuntu12.04下使用ping命令返回ping:icmp open socket: Operation not permitted的解决
ping命令在运行中采用了ICMP协议,需要发送ICMP报文.但是只有root用户才能建立ICMP报文.而正常情况下,ping命令的权限应为-rwsr-xr-x,即带有suid的文件,一旦该权限被修改 ...
- 我与python3擦肩而过(一)—— Dict与collections.OrderredDict邂逅
最近一直在撸Python Data Analysis上的代码(书是基于Python2的,小白我用的python3),所以我下的时候多少有些改动. 这是9.4中的nltk词频分析关于Dict_key的问 ...
- PHP中函数的使用
函数是一种可以在任何被需要的时候执行的代码块函数的定义 1.函数是一个被命名的独立的代码段 2.它执行特殊的任务 3.并可以给调用它的程序返回值 函数的优点: 1.提高程序的重 ...
- More on Conditions - To Compare -Comparing Sequences and Other Types
The conditions used in while and if statements can contain any operators, not just comparisons. The ...
- 浅谈Android应用性能之内存
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/ jaunty [博主导读]在Android开发中,不免会遇到许多OOM现象,一方面可能是由于开 ...
- C#_控件——DropDownList
1.html <asp:CheckBox ID="CheckBox11" runat="server" onclick="changecheck ...