poj 2446 Chessboard (二分匹配)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12800 | Accepted: 4000 |
Description

We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below:
1. Any normal grid should be covered with exactly one card.
2. One card should cover exactly 2 normal adjacent grids.
Some examples are given in the figures below:
A VALID solution.
An invalid solution, because the hole of red color is covered with a card.
An invalid solution, because there exists a grid, which is not covered.
Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.
Input
Output
Sample Input
4 3 2
2 1
3 3
Sample Output
YES
Hint
A possible solution for the sample input.
Source
和 hdu 1507类似,构无向图然后判断匹配数是否等于合法的格数。
心算32*32错了= = RE了两次,开始以为32*32是90+,第二次以为是900+,笔算后才知道是1024..
//224K 125MS C++ 1731B 2014-06-10 12:44:41
#include<iostream>
#include<vector>
#define N 1050
using namespace std;
vector<int>V[N];
int match[N];
int vis[N];
int g[][];
int dfs(int u)
{
for(int i=;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=;
if(match[v]==- || dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary(int n)
{
int ret=;
memset(match,-,sizeof(match));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
int n,m,k,x,y;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<N;i++) V[i].clear();
for(int i=;i<k;i++){
scanf("%d%d",&y,&x);
g[x-][y]=;
}
int map[N]={},pos=;
for(int i=;i<n;i++)
for(int j=;j<=m;j++)
if(!g[i][j]){
if(!map[i*m+j]) map[i*m+j]=++pos;
int u=map[i*m+j];
if(j<m && !g[i][j+]){
if(!map[i*m+j+]) map[i*m+j+]=++pos;
V[u].push_back(map[i*m+j+]);
V[map[i*m+j+]].push_back(u);
}
if(i<n- && !g[i+][j]){
if(!map[(i+)*m+j]) map[(i+)*m+j]=++pos;
V[u].push_back(map[(i+)*m+j]);
V[map[(i+)*m+j]].push_back(u);
}
}
//printf("%d\n",pos);
if(hungary(pos)==pos) puts("YES");
else puts("NO");
}
return ;
}
poj 2446 Chessboard (二分匹配)的更多相关文章
- POJ 2446 Chessboard (二分图匹配)
题意 在一个N*M的矩形里,用1*2的骨牌去覆盖该矩形,每个骨牌只能覆盖相邻的两个格子,问是否能把每个格子都盖住.PS:有K个孔不用覆盖. 思路 容易发现,棋盘上坐标和为奇数的点只会和坐标和为偶数的点 ...
- poj 2446 Chessboard (二分图利用奇偶性匹配)
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13176 Accepted: 4118 Descr ...
- POJ 3041 - 最大二分匹配
这道题实现起来还是比较简单的,但是理解起来可能有点困难. 我最开始想到的是贪心法,每次消灭当前小行星最多的一行或一列.然而WA了.Discuss区里已经有高人给出反例. 下面给出正确的解法 我们把行和 ...
- POJ 2446 Chessboard (二分图最大匹配)
题目链接:http://poj.org/problem?id=2446 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个 ...
- POJ 2446 Chessboard
要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...
- POJ 2446 Chessboard【二分图最大匹配】
<题目链接> 题目大意: 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个棋盘是否能被纸片填满. 解题分析: ...
- POJ 3057 Evacuation (二分匹配)
题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...
- POJ 2289 多重二分匹配+二分 模板
题意:在通讯录中有N个人,每个人能可能属于多个group,现要将这些人分组m组,设各组中的最大人数为max,求出该最小的最大值 下面用的是朴素的查找,核心代码find_path复杂度是VE的,不过据说 ...
- POJ 1469 ZOJ1140 二分匹配裸题
很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...
随机推荐
- 由javascript中的this指针所想到的
初次结识 this 指针,是在学 <<C++ Primer Plus>>这本书的时候(这本书勉强读了一二遍,之后转学 html+css+js了,不过这是后话). 依稀记得书中举 ...
- Python chr() ord() unichr()
chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符. unichr()跟它一样,只不过返回的是Unicode字符,这个从Python 2.0才加入的un ...
- activiti自定义流程之整合(五):启动流程时获取自定义表单
流程定义部署之后,自然就是流程定义列表了,但和前一节一样的是,这里也是和之前单独的activiti没什么区别,因此也不多说.我们先看看列表页面以及对应的代码,然后在一步步说明点击启动按钮时如何调用自定 ...
- python中获取当前所有的logger
获得当前所有logger的列表的程序如下: import logging for name in logging.Logger.manager.loggerDict.keys(): logger = ...
- tensorflow 学习1 环境搭建
1. 下载Anaconda(首选官网,但很慢) Anaconda2-4.0.0-Linux-x86_64.sh Anaconda 安装包还可以到 https://mirrors.tuna.tsingh ...
- GitHub上一个不错的开源C#源码(控制台界面开发)
https://github.com/flagbug/FlagConsole 如链接所示
- NSIS打包(一)常用概念简介
1.NSIS简介 官网:http://sourceforge.net/projects/nsis/ 维基百科: http://zh.wikipedia.org/wiki/Nullsoft%E8%85% ...
- (转)SQLServer_T-SQL 语句执行时间的查询
原文地址:http://blog.csdn.net/wangjunhe/article/details/7211974 --利用getdatedeclare @beginTime datetime d ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- linux下一对多socket服务器端多线程泄露问题
线程创建多了,没有释放.导致内存泄露... int main() { int len; int on=1; // pMachList = CreateEmptyLinklist(); DataBase ...