HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507
题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输出任一组匹配方案。
解题思路:按行列和奇偶划分两个集合,从而得到二分图,然后进行最大匹配,根据link数组的匹配结果输出相应匹配即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1e2+; int n,m,cnt,idx;
int mp[N][N],link[N],head[N];
bool vis[N];
int d[][]={,,,,-,,,-}; struct node{
int x,y;
node(){}
node(int x,int y):x(x),y(y){}
}a[N*N]; struct enode{
int to,next;
}edge[N*N]; void init(){
cnt=;
idx=;
memset(mp,,sizeof(mp));
memset(head,,sizeof(head));
} void addedge(int u,int v){
edge[idx].to=v;
edge[idx].next=head[u];
head[u]=idx++;
} bool dfs(int u){
for(int i=head[u];i;i=edge[i].next){
int t=edge[i].to;
if(!vis[t]){
vis[t]=true;
if(link[t]==-||dfs(link[t])){
link[t]=u;
return true;
}
}
}
return false;
} int max_match(){
memset(link,-,sizeof(link));
int ans=;
for(int i=;i<=cnt;i++){
memset(vis,false,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
} int main(){
while(~scanf("%d%d",&n,&m)&&n&&m){
init();
int k;
scanf("%d",&k);
for(int i=;i<=k;i++){
int x,y;
scanf("%d%d",&x,&y);
mp[x][y]=-;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]!=-){
mp[i][j]=++cnt;
a[cnt]=node(i,j);
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
//加了(i+j)%2这句分奇偶就对了,不知道为什么不分会错。。。。
if(mp[i][j]!=-&&(i+j)%==){
for(int k=;k<;k++){
int x=i+d[k][];
int y=j+d[k][];
if(x<=||y<=||x>n||y>m||mp[x][y]==-) continue;
addedge(mp[i][j],mp[x][y]);
}
}
}
}
printf("%d\n",max_match());
vector<node>ans;
for(int i=;i<=cnt;i++){
if(link[i]!=-){
ans.push_back(node(i,link[i]));
link[i]=link[link[i]]=-;
}
}
for(int i=;i<ans.size();i++){
int p1=ans[i].x,p2=ans[i].y;
printf("(%d,%d)--(%d,%d)\n",a[p1].x,a[p1].y,a[p2].x,a[p2].y);
}
printf("\n");
}
return ;
}
HDU 1507 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 ...
- HDU 1507 Uncle Tom's Inherited Land*
题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...
- 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 ...
- 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 ...
- 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 ...
- ZOJ1516 Uncle Tom's Inherited Land(二分图最大匹配)
一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个 ...
随机推荐
- ural 2032 Conspiracy Theory and Rebranding (数学水题)
ural 2032 Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 ...
- Lvs+Keepalived实现MySQL高可用
LVS+Keepalived+MySQL高可用配置 本文所有配置前提是已实现MySQL双主备份(MySQL双主) 安装前的准备: VIP:192.168.0.201 Keepalived: Keepa ...
- P1776 宝物筛选_NOI导刊2010提高(02)&& 多重背包二进制优化
多重背包, 要求 \(N\log N\) 复杂度 Solution 众所周和, \(1-N\) 之内的任何数可以由 \(2^{0}, 2^{1}, 2^{2} ... 2^{\log N}, N - ...
- mongo ttl索引
db.log_events.find() # 查找log_events里的所有数据 db.log_events.create ...
- okhttp 简单用法
1.gradle 依赖 github 中查找最新的 2.MyApplication oncreate 中: @Override public void onCreate () { super.onCr ...
- Java并发编程原理与实战二十四:简易数据库连接池
public class MyDataSource { private static LinkedList<Connection> pool = new LinkedList<> ...
- Java并发编程原理与实战二十三:Condition原理分析
先来回顾一下java中的等待/通知机制 我们有时会遇到这样的场景:线程A执行到某个点的时候,因为某个条件condition不满足,需要线程A暂停:等到线程B修改了条件condition,使condit ...
- 用JS获得QQ号码的昵称,头像,生日
有一个网址,可以返回我们要的内容. http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin=指定QQ号码 将会返回下列内容: _Callbac ...
- MySQL中JSON字段的使用技巧
mysql5.7.8之后开始原生支持json. 在类似mongodb这种nosql数据库中,json存储数据是非常自然的, 在mysql中合理的使用json,能够带来极大的便利 Json字段的使用场景 ...
- 30、hashCode方法
HashCode方法的作用 在HashSet中的元素是不能重复的,jvm可以通过equals方法来判断两个对象是否相同,假设自定义一个Person类里面有10个成员变量,每调用一次equals方法需要 ...