「日常训练」Uncle Tom's Inherited Land*(HDU-1507)
题意与分析
题意是这样的:给你一个\(N\times M\)的图,其中有一些点不能放置\(1\times 2\)大小的矩形,矩形可以横着放可以竖着放,问剩下的格子中,最多能够放多少个矩形。
注意到是\(1\times 2\)的矩形,所以是一个\(i+j\)和为奇数的可以与\(i+j\)为偶数的相连。抽象坐标和分别为奇数、偶数的为二分图的两个点集,可构建的矩形为边,那么剩下要做的就是二分图的最大匹配。
比较有趣的是具体实现。先把可以匹配的点单独拎出来,然后根据这些点建图。具体怎么建的呢?给每个可以匹配的点标记,如果这些点是奇数点,那么它周围的点连一条边。然后完事了。
然后如何打印结果呢?注意到Linker数组是保存了每一个点与其他点匹配的信息的,拿出来打印就是了。
这题重要的是这个奇偶的建图思想。
代码
/* ACM Code written by Sam X or his teammates.
* Filename: hdu1507.cpp
* Date: 2018-11-16
*/
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define PB emplace_back
#define MP make_pair
#define fi first
#define se second
#define rep(i,a,b) for(repType i=(a); i<=(b); ++i)
#define per(i,a,b) for(repType i=(a); i>=(b); --i)
#define ZERO(x) memset(x, 0, sizeof(x))
#define MS(x,y) memset(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define QUICKIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define DEBUG(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
using namespace std;
using pi=pair<int,int>;
using repType=int;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
int n,m,uN,vN;
int a[105][105], b[105], g[505][505];
bool used[505];
int linker[505];
bool dfs(int u)
{
rep(v,0,vN-1)
if(g[u][v] && !used[v])
{
used[v]=true;
if(linker[v]==-1 || dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
}
int hungary()
{
int res=0;
MS(linker,-1);
rep(u,0,uN-1)
{
ZERO(used);
if(dfs(u)) res++;
}
return res;
}
int
main()
{
while(cin>>n>>m)
{
if(!n && !m) break;
int k; cin>>k;
ZERO(a);
while(k--)
{
int u,v; cin>>u>>v;
a[u-1][v-1]=-1;
}
int idx=0;
rep(i,0,n-1)
rep(j,0,m-1)
{
if(a[i][j]!=-1)
{
b[idx]=i*m+j;
a[i][j]=idx++;
}
}
uN=vN=idx;
ZERO(g);
rep(i,0,n-1)
rep(j,0,m-1)
{
if(a[i][j]!=-1 && (i+j)%2)
{
int u=a[i][j];
if(i>0 && a[i-1][j]!=-1)
g[u][a[i-1][j]]=1;
if(i<n-1 && a[i+1][j]!=-1)
g[u][a[i+1][j]]=1;
if(j>0 && a[i][j-1]!=-1)
g[u][a[i][j-1]]=1;
if(j<m-1 && a[i][j+1]!=-1)
g[u][a[i][j+1]]=1;
}
}
int ans=hungary();
cout<<ans<<endl;
rep(i,0,vN-1)
{
if(linker[i]!=-1)
{
int x1=b[i]/m+1,
y1=b[i]%m+1,
x2=b[linker[i]]/m+1,
y2=b[linker[i]]%m+1;
cout<<"("<<x1<<","<<y1<<")--("<<x2<<","<<y2<<")\n";
}
}
cout<<endl;
}
return 0;
}
「日常训练」Uncle Tom's Inherited Land*(HDU-1507)的更多相关文章
- 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*(二分图匹配)
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 ...
- 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 ...
- XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*
B. Uncle Tom's Inherited Land* Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I ...
- 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...
- 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 ...
- 「日常训练」 Fire!(UVA-11624)
与其说是训练不如说是重温.重新写了Java版本的代码. import java.util.*; import java.math.*; import java.io.BufferedInputStre ...
随机推荐
- Unity中自定义扩展方法
问题背景 在使用unity开发过程中,通常会遇到一种情况,比如说给物体重新赋值坐标的问题, Transfrom tran: ,pos_y=,pos_z=; tran.position=new Vect ...
- java _this关键字的用法
1:This关键字可以用于从一个构造方法调用另一个构造方法,可以用于避免重复代码 2:this的第二个用于this.xxx表示成员变量,成员变量的作用范围是 类 避免产生歧义 package c ...
- 【SQLSERVER学习笔记】细节记录
SQLSERVER 查询时,WHERE中使用<>时,不会把NULL值查出来. SQLSERVER子查询中不能使用 ORDER BY. SQLSERVER 使用DISTINCT时,必须把OR ...
- Mahout介绍
3.11简介 Mahout:是一个Apache的一个开源的机器学习库,主要实现了三大类算法Recommender (collaborative filtering).Clustering.classi ...
- 使用dbca命令静默卸载数据库
1) help查询dbca的选项 su - oracledbca -help dbca [-silent | -progressOnly | -customCreate] {<comma ...
- java 计算器算法脚本
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public String Count(Strin ...
- Vue性能优化之组件按需加载(以及一些常见的性能优化方法)
关于Vue中的按需加载我就简单介绍一下:大概就是我们所有的东西都会在app.js里面,但是我们并不需要把所有的组件都一次性加载进来,我们可以在需要它的时候再将它加载进来,话不多说,开车! 1.webp ...
- ORALCE表的约束条件
一.主键:(PRIMARY KEY) 主键是表中的一列或多列.为表定义主键有如下几个作用: 1.主键包含的列不能输入重复的值,以此来保证一个表的所有行的唯一性: 2.主键也不允许定义此约束的列为NUL ...
- composer切换中国镜像
替换 composer.lock 文件中的 https://files.phpcomposer.com/files/ 为 https://dl.laravel-china.org 命令行 compos ...
- WIN10下WNMP开发环境部署
刚刚开始学习PHP时,一直使用phpstudy,后面发现很多东西自己单独配置安装会理解更深刻,所以自己总结了一下windows下开发环境的部署教程. 以前经常在CSDN和博客园看别人的教程,今天才注册 ...