XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*
B. Uncle Tom's Inherited Land*
64-bit integer IO format: %I64d Java class name: Main
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
Output
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) 解题:根据奇偶性建图+最大匹配数+最大匹配数的记录。根据奇偶性建图:在一个平面坐标系中,某个格子的 纵横坐标和 与其相领格子的 纵横坐标和 的绝对值相差1,即相差一个数。由于奇数与偶数相间分布,故只选择纵横坐标和为奇数或者纵横坐标和为偶数的点进行建图(从始至终只能选择其中一种方式),这样保证了是一个二分图。 这题的写法,把原先的匈牙利算法的线段的端点的由的写成了二维的,算法还是那样的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct{
int x,y;
}link[maxn][maxn];
int n,m;
bool mp[maxn][maxn],vis[maxn][maxn];
bool isIn(int x,int y){
if(x < || x > n || y < || y > m)
return false;
return true;
}
bool dfs(int x,int y){
static const int dir[][] = {{,},{-,},{,},{,-}};
for(int i = ; i < ; i++){
int tx = x+dir[i][];
int ty = y+dir[i][];
if(isIn(tx,ty) && !vis[tx][ty] && !mp[tx][ty]){
vis[tx][ty] = true;
if(link[tx][ty].x == - || dfs(link[tx][ty].x,link[tx][ty].y)){
link[tx][ty].x = x;
link[tx][ty].y = y;
return true;
}
}
}
return false;
}
int main(){
int i,j,u,v,k;
while(scanf("%d%d",&n,&m),n+m){
scanf("%d",&k);
memset(mp,false,sizeof(mp));
while(k--){
scanf("%d%d",&u,&v);
mp[u][v] = ;
}
int ans = ;
memset(link,-,sizeof(link));
for(i = ; i <= n; i++){
for(j = ; j <= m; j++){
memset(vis,false,sizeof(vis));
if(((i+j)&) && !mp[i][j] && dfs(i,j)) ans++;
}
}
printf("%d\n",ans);
for(i = ; i <= n; i++){
for(j = ; j <= m; j++){
if(link[i][j].x != -)
printf("(%d,%d)--(%d,%d)\n",link[i][j].x,link[i][j].y,i,j);
}
}
printf("\n");
}
return ;
}
XTU 二分图和网络流 练习题 B. 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 ...
- Uncle Tom's Inherited Land*
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 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 ...
- XTU 二分图和网络流 练习题 J. Drainage Ditches
J. Drainage Ditches Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Ja ...
- XTU 二分图和网络流 练习题 C. 方格取数(1)
C. 方格取数(1) Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Java class ...
- ZOJ1516 Uncle Tom's Inherited Land(二分图最大匹配)
一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个 ...
随机推荐
- iOS中数据类型转换--遇到则记录
1.NSString转NSNumber 使用情景:CoreData存储数据,其中一个为价格,CoreData里面定义为float 用文本输入框得到的数据类型是NSString,将NSString转换成 ...
- Backbone.js入门教程第二版笔记(2)
关于手动触发router,之前看到的例子都是通过在url后面加上#xxx或者点击一个a链接方法来触发, 还有一种情况是通过触发一种规则,来触发另一种规则(表述无能),比如这个例子中,我在url后面加上 ...
- PV,UV,IP概念
PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量.对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量,如同一个来访者 ...
- 洛谷P3254 圆桌问题(最大流)
题意 $m$个不同单位代表参加会议,第$i$个单位有$r_i$个人 $n$张餐桌,第$i$张可容纳$c_i$个代表就餐 同一个单位的代表需要在不同的餐桌就餐 问是否可行,要求输出方案 Sol 比较zz ...
- viewport实现html页面动态缩放/meta viewport/viewport
页面默认缩放比例为1,最小宽度为375px,在小于375px出现水平滚动条的时候重新计算显示比例缩小界面, <!DOCTYPE html> <html lang="en&q ...
- elasticsearch 2.4在head删除数据(使用Delete By Query插件)
之所以说明是2.4版,是因为不同版本删除的语法不一样(例如跟5.x就不同) 首先安装Delete By Query插件,方式跟安装head插件差不多,安装命令:plugin install delet ...
- ios MD5大小写加密
#import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...
- Android获取本地相册图片、拍照获取图片
需求:从本地相册找图片,或通过调用系统相机拍照得到图片. 容易出错的地方: 1,当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量 ...
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- Java练习题00
问题: 将一个数字与数组中的元素比较, 如果该数字存在数组中,给出该数字在数组中的位置: 如果该数字不在数组中,给出提示. 代码: public class Page84{ public ...