ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配)

2023-11-21 21:18

本文主要是介绍ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1516

Your 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).


Input


Input 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.


<b< dd="">

Output


For each test case in the input your program should produce one line of output, containing an integer value representing the maximum number of properties which can be sold.


<b< dd="">

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


<b< dd="">

Sample Output

4
3


   题意:n*m的土地上,求有多少1*2 或者 2*1 的块数 ,池塘不能算在内.

   思路:每一个不是池塘的土地,都可以跟他的上下左右四个方向上不是池塘的块构成一个1*2或者2*1的长方形块,把所有连通的块存起来,构成一个二部图,剩下的就是求最大匹配.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 110
int g[MAXN][MAXN];
int mp[MAXN][MAXN];
int node[MAXN][MAXN];
int cx[MAXN],cy[MAXN];
int sx[MAXN],sy[MAXN];
int vis[MAXN];
int n,m,num,ans;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
void bulid()
{int i,j,k;num=1;for(i=1; i<=n; i++)for(j=1; j<=m; j++){if(!mp[i][j])node[i][j]=num++;}for(i=1; i<=n; i++)for(j=1; j<=m; j++){if(!mp[i][j]){for(k=0; k<4; k++){int tx=i+dir[k][0];int ty=j+dir[k][1];if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&mp[tx][ty]==0){g[node[i][j]][node[tx][ty]]=1;g[node[tx][ty]][node[i][j]]=1;}}}}
}
int path(int u)
{sx[u]=1;int v;for(v=1; v<=num; v++){if(g[u][v]&&!sy[v]){sy[v]=1;if(!cy[v]||path(cy[v])){cx[u]=v;cy[v]=u;return 1;}}}return 0;
}
int MaxMatch()
{int i;ans=0;memset(cx,0,sizeof(cx));memset(cy,0,sizeof(cy));for(i=1; i<=num; i++){if(!cx[i]){memset(sx,0,sizeof(sx));memset(sy,0,sizeof(sy));ans+=path(i);}}return 0;
}
int main()
{int i,j,k,u,v;while(~scanf("%d%d",&n,&m)&&n+m){memset(mp,0,sizeof(mp));memset(g,0,sizeof(g));memset(node,0,sizeof(node));scanf("%d",&k);for(i=0; i<k; i++){scanf("%d%d",&u,&v);mp[u][v]=1;}bulid();MaxMatch();printf("%d\n",ans/2);}return 0;
}

这篇关于ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405157

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta

poj 3692 二分图最大独立集

题意: 幼儿园里,有G个女生和B个男生。 他们中间有女生和女生认识,男生男生认识,也有男生和女生认识的。 现在要选出一些人,使得这里面的人都认识,问最多能选多少人。 解析: 反过来建边,将不认识的男生和女生相连,然后求一个二分图的最大独立集就行了。 下图很直观: 点击打开链接 原图: 现图: 、 代码: #pragma comment(

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边

hdu 3065 AC自动机 匹配串编号以及出现次数

题意: 仍旧是天朝语题。 Input 第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。 接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。 在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。