hdu1507——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): 2114 Accepted Submission(s): 867
Special Judge
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).

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.
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.
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
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)
二分匹配。建边的根据是枚举全部可行的点,然后枚举周围四个方向。输出方案根据mark数组
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn = 10010;
struct node
{
int to;
int next;
}edge[4 * maxn]; int vis[maxn];
int head[maxn];
int mark[maxn];
bool used[maxn];
bool mat[110][110];
int cnt[110][110];
int tot;
int n, m;
int index; void addedge(int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
} bool dfs(int x)
{
for (int i = head[x]; i != -1; i = edge[i].next)
{
if (!used[edge[i].to])
{
used[edge[i].to] = 1;
if (mark[edge[i].to] == -1 || dfs(mark[edge[i].to]))
{
mark[edge[i].to] = x;
return true;
}
}
}
return false;
} int hungary()
{
memset(mark, -1, sizeof(mark));
int ans = 0;
for (int i = 0; i < index; i++)
{
memset(used, 0, sizeof(used));
if (dfs(i))
ans++;
}
return ans;
} int main()
{
int k;
while (~scanf("%d%d", &n, &m))
{
if (!n && !m)
{
break;
}
scanf("%d", &k);
map<int, int>qu;
qu.clear();
memset(head, -1, sizeof(head));
memset(mat, false, sizeof(mat));
memset(vis, -1, sizeof(vis));
tot=0;
int x, y;
for (int i = 0; i < k; i++)
{
scanf("%d%d", &x, &y);
x--;
y--;
mat[x][y] = true;
}
index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!mat[i][j])
{
cnt[i][j] = index++;
qu[index - 1] = i * m + j;
} for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (!mat[i][j])
{
if (i > 0 && !mat[i - 1][j])
addedge(cnt[i][j], cnt[i - 1][j]);
if (i < n-1 && !mat[i + 1][j])
addedge(cnt[i][j], cnt[i + 1][j]);
if (j > 0 && !mat[i][j - 1])
addedge(cnt[i][j], cnt[i][j - 1]);
if (j < m - 1 && !mat[i][j + 1])
addedge(cnt[i][j], cnt[i][j + 1]);
}
}
int res = hungary();
printf("%d\n", res / 2);
for (int i = 0; i < index; ++i)
{
if (mark[i] != -1)
{
int a = qu[i];
int b = qu[mark[i]];
int x1 = a / m + 1;
int y1 = a % m + 1;
int x2 = b / m + 1;
int y2 = b % m + 1;
if (vis[a] == -1 && vis[b] == -1)
{
printf("(%d,%d)--(%d,%d)\n", x1, y1, x2, y2);
vis[a] = b;
vis[b] = a;
}
}
}
printf("\n");
}
return 0;
}
hdu1507——Uncle Tom's Inherited Land*的更多相关文章
- HDOJ 1507 Uncle Tom's Inherited Land*
直接对每一个格子进行dfs结果除以2能够得到答案可是有大量反复的结果,不好输出答案. 能够仅仅对横纵坐标相加是奇数的格子dfs.... Uncle Tom's Inherited Land* Time ...
- ZOJ 1516 Uncle Tom's Inherited Land(二分匹配 最大匹配 匈牙利啊)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 Your old uncle Tom inherited a p ...
- 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 ...
- hdu1507 Uncle Tom's Inherited Land* 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 将i+j为奇数的构成x集合中 将i+j为偶数的构成y集合中 然后就是构建二部图 关键就是构图 然 ...
- HDU1507 Uncle Tom's Inherited Land*
题目是跟 zoj1516是一样的,但多了匹配后的输出 详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html #include & ...
- 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 ...
- Uncle Tom's Inherited Land*
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 移动web touch事件
参考:http://www.cnblogs.com/dojo-lzz/p/5452575.html wap中的原生touch 事件,touchstart.touchmove.touchend.touc ...
- 如何用纯 CSS 绘制一颗闪闪发光的璀璨钻石
效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. 在线演示 https://codepen.io/zhang-ou/pen/qYqwQp 可交互视频教程 此视 ...
- ps---打开文件及图片保存格式
1.打开图片,可以按Ctrl或者Shift来进行多张图片的选择或者用鼠标框选. 2.勾选图像序列,可以选择命名上有次序的多个图像. 3. PSD是ps里面的标准保存格式,包含颜色.图层.通道.路径.动 ...
- php 快速导出大量CSV文件
原文链接 https://segmentfault.com/a/1190000005366832 /** * 导出excel(csv) * @data 导出数据 * @headlist 第一行,列名 ...
- php 数据库的增删改查
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>&l ...
- HDU2069-Coin Change
Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...
- 如何在Eclipse中生成Native类对应的JNI的.h文件
1 致谢 感谢super_level网友 他的博客写的很清楚 给了我很多帮助 链接如下:http://blog.csdn.net/super_level/article/details/2124353 ...
- angularjs ngRoute的使用简单例子
很丑的小例子,刚学angularjs,写下来方面以后看. 1.例子的工程目录如下: 2.index.html代码如下: <!DOCTYPE html><html><hea ...
- eclipse菜单字体乱码的解决
方法一: 这个跟活动控制台代码页有关. 如果要更改为 UTF-8,则需要运行 chcp 命令: chcp 65001 有时新安装的系统可能在运行一些中文软件时显示错乱,可通过控制面板修改系统区域来管理 ...
- Tomcat可以实现Session共享方案
说明:原来Tomcat也是可以实现Session共享的,这样大大减少的硬编码的实现,并且前面用Nginx分流时不用考虑Session的问题,因为是Web容器提供了Session共享的支持. 1.在每个 ...