HDU1507 Uncle Tom's Inherited Land*
题目是跟 zoj1516是一样的,但多了匹配后的输出
详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespace std; 5 6 const int maxn = 55; 7 int n , m , k; 8 int g[maxn][maxn] , cx[maxn] , cy[maxn] , visy[maxn] , nx , ny; 9 bool col[105][105];//判断是否为鱼塘 10 11 struct Point{ 12 int x , y; 13 Point(int x=0 , int y=0):x(x),y(y){} 14 }; 15 16 Point px[maxn] , py[maxn]; 17 18 bool ok(Point p1 , Point p2) 19 { 20 if((p1.x == p2.x && abs(p1.y - p2.y) == 1) || (p1.y == p2.y && abs(p1.x - p2.x) == 1)) 21 return true; 22 return false; 23 } 24 25 void build_graph() 26 { 27 memset(g , 0 , sizeof(g)); 28 for(int i=1 ; i<=nx ; i++){ 29 for(int j=1 ; j<=ny ; j++){ 30 if(ok(px[i] , py[j])) 31 g[i][j] = 1; 32 } 33 } 34 } 35 36 int dfs(int u) 37 { 38 for(int v = 1 ; v<=ny ; v++) 39 if(g[u][v] && !visy[v]){ 40 visy[v] = 1; 41 if(cy[v] == -1 || dfs(cy[v])){ 42 cx[u] = v; 43 cy[v] = u; 44 return 1; 45 } 46 } 47 return 0; 48 } 49 50 int MaxMatch() 51 { 52 memset(cx , -1 , sizeof(cx)); 53 memset(cy , -1 , sizeof(cy)); 54 int ans = 0; 55 for(int i=1 ; i<=nx ; i++){ 56 if(cx[i] == -1){ 57 memset(visy , 0 , sizeof(visy)); 58 ans += dfs(i); 59 } 60 } 61 return ans; 62 } 63 64 int main() 65 { 66 // freopen("a.in" , "r" ,stdin); 67 while(scanf("%d%d" , &n , &m) , n) 68 { 69 scanf("%d" , &k); 70 int x , y; 71 memset(col , 0 , sizeof(col)); 72 while(k--){ 73 scanf("%d%d" , &x , &y); 74 col[x][y] = 1; 75 } 76 nx = 0 , ny = 0; 77 for(int i=1 ; i<=n ; i++) 78 for(int j=1 ; j<=m ; j++) 79 if(!col[i][j]){ 80 if((i+j)&1) py[++ny] = Point(i,j); 81 else px[++nx] = Point(i,j); 82 } 83 //构造二分图 84 build_graph(); 85 printf("%d\n" , MaxMatch()); 86 for(int i=1 ; i<=nx ; i++){ 87 if(cx[i] != -1) 88 printf("(%d,%d)--(%d,%d)\n",px[i].x , px[i].y , py[cx[i]].x , py[cx[i]].y); 89 } 90 } 91 return 0; 92 }
转载于:https://www.cnblogs.com/CSU3901130321/p/4228093.html
HDU1507 Uncle Tom's Inherited Land*相关推荐
- hdu 1507 Uncle Tom's Inherited Land*
这两天刚学二分图,对二分图的应用还不熟练;感觉自己还是太弱了,这题是参考别人代码的; 这题是把(i+j)的值得奇偶性来分图的,请原谅我没有想到,还是太水了..根本没有思路.. 先用一个数组暂时存起鱼塘 ...
- ZOJ1516 Uncle Tom's Inherited Land(二分图最大匹配)
一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个 ...
- ZOJ 1516 Uncle Tom's Inherited Land
题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻 ...
- HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输 ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
- 一系列图论问题[转]
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 杭电OJ分类题目(4)-Graph
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...
- c语言大小写字母互换1005,1005 Jugs,1005jugs
1005 Jugs,1005jugs 辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. 1 #include 2 3 intmain(){4 intA,B,T,sA,sB;5 while(sca ...
最新文章
- BCH半月热点事件回顾(第二期)
- python中 s是什么意思_什么是Pandas?Pandas学习什么?
- 杭电oj2001题解
- My github blog
- GDC2017分享:移动VR开发者的赚钱之道
- 中国体声波器件市场趋势报告、技术动态创新及市场预测
- 如何备份数据库的某张表_详解mysql数据库如何用mysqldump实现单表备份
- oracle还原数据库方法,oracle数据库备份与还原(命令与方法)
- 塑料成型计算机模拟技术,塑料成型模拟软件技术基础与应用
- AntV G6设置高亮
- bada打地鼠应用程序简介
- css图片背景之雪碧图的使用
- 搭建:LVS+Keepalived高可用Web服务群集环境
- shell 编程四剑客简介 find sed grep awk(微信公众号摘抄)
- python 与and运算符
- Mac npm6.10.3包管理器下载依赖并运行项目的步骤
- Houdini to ue4 入门笔记 第一章
- python四级成绩_python 批量爬取四级成绩单
- uniapp无法唤起相机的避坑之旅(安卓10以上,以及鸿蒙手机)
- 数据迁移测试经验总结