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): 1247 Accepted Submission(s): 542
Special Judge
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).
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)
要输出任意一组解。
一开始时两边都是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*(二分匹配,输出任意一组解)的更多相关文章
- 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 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 ...
- 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的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...
- hdu1507 Uncle Tom's Inherited Land* 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 将i+j为奇数的构成x集合中 将i+j为偶数的构成y集合中 然后就是构建二部图 关键就是构图 然 ...
- 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://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...
- Uncle Tom's Inherited Land*
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 内存分配器memblock【转】
转自:http://blog.csdn.net/kickxxx/article/details/54710243 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 背景 Data ...
- python基础===时间处理模块
时间模块 Python中有很多方便我们处理时间信息的模块 time 模块 datetime 模块 pytz 模块 dateutil 模块 这里我们着重介绍的是前两种 time模块 time.time( ...
- macaca安装失败的解决办法!
https://github.com/macacajs/macaca-android https://www.jianshu.com/p/76a5be6c1036
- 【bzoj4530】大融合(LCT的子树维护)
LCT维护子树并没有想象中的那么难,在这里只是复习下. (其他的维护子树的题目,可见:“共价大爷游长沙”) 只要记录下虚边连接的信息就好了. #include<bits/stdc++.h> ...
- IoT之车联网
一. 背景 这是一个笔者在实习公司策划的关于车联网的小项目,也是笔者参加某竞赛的作品<基于云平台的车内滞留儿童状况监测与处理>,本项目旨在为因各种原因导致儿童滞留车内热死.闷死的社会性事件 ...
- rabbitmq和ons-rocketmq使用对比
MQ,其实目的都是一样,就是应对系统的并发可以实现消峰和解耦的作用,类似于创建了一个线程异步操作,这一点可以了解一下一款优秀的并发框架(Disruptor),据说是每秒可以处理十万级数据, 目前据本人 ...
- KettleDB连接jdbc连接池参数介绍
http://sheng8407-sina-com.iteye.com/blog/1163245 http://blog.csdn.net/dingxingmei/article/details/41 ...
- DevExpress.XtraTreeList 小结
搞了半天才绑定好,没有弄清楚父子之间的关系 <dx:ASPxTreeList ID="ASPxTreeList1" runat="server" Auto ...
- FineReport——插入行策略
1.空值是默认的选项,即每次插入新行时,格子都是空白的. 2.原值即单元格中原有内容是什么,就复制到新增的格子中,一般适用于单元格是使用公式定义的, 在插入单元格时,公式会保留下来. 3.默认值即通过 ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...