Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13176   Accepted: 4118

Description

Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the
figure below). 




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

There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.

Output

If the board can be covered, output "YES". Otherwise, output "NO".

Sample Input

4 3 2
2 1
3 3

Sample Output

YES

Hint

 

A possible solution for the sample input.

给你一个棋盘,棋盘上有几个洞。要求你用一些1*2的卡片覆盖没有洞的区域,一个格子仅仅能有一张卡片覆盖。

看能否恰好覆盖。





二分匹配:利用二分匹配。两个能匹配的格子的坐标和必定奇偶性不同。利用这一点能够降低时间耗费。

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 35
#define M 1200
int g[N][N],n,m;
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int mark[M],link[M];
int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
int find(int k)
{
int i,j,x,y,di,dj;
x=k/m;
y=k%m;
for(i=0;i<4;i++)
{
di=dir[i][0]+x;
dj=dir[i][1]+y;
if(judge(di,dj)&&!g[di][dj])
{
j=di*m+dj;
if(!mark[j])
{
mark[j]=1;
if(link[j]==-1||find(link[j]))
{
link[j]=k;
return 1;
}
}
}
}
return 0;
}
int main()
{
int u,v,k,i,j;
while(scanf("%d%d%d",&n,&m,&k)!=-1)
{
memset(g,0,sizeof(g));
for(i=0;i<k;i++)
{
scanf("%d%d",&v,&u);
u--;v--;
g[u][v]=1;
}
if((n*m-k)&1)
{
printf("NO\n");
continue;
}
int ans=0;
memset(link,-1,sizeof(link));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if((i+j)%2==0||g[i][j]) //(i+j)奇偶性! !!
continue;
memset(mark,0,sizeof(mark));
ans+=find(i*m+j);
}
}
//printf("%d\n",ans);
if(ans*2==n*m-k)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

poj 2446 Chessboard (二分图利用奇偶性匹配)的更多相关文章

  1. POJ 2446 Chessboard (二分图匹配)

    题意 在一个N*M的矩形里,用1*2的骨牌去覆盖该矩形,每个骨牌只能覆盖相邻的两个格子,问是否能把每个格子都盖住.PS:有K个孔不用覆盖. 思路 容易发现,棋盘上坐标和为奇数的点只会和坐标和为偶数的点 ...

  2. POJ 2446 Chessboard (二分图最大匹配)

    题目链接:http://poj.org/problem?id=2446 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个 ...

  3. poj 2446 Chessboard (二分匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12800   Accepted: 4000 Descr ...

  4. POJ 2446 Chessboard【二分图最大匹配】

    <题目链接> 题目大意: 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个棋盘是否能被纸片填满. 解题分析: ...

  5. POJ 2446 Chessboard(二分图最大匹配)

    题意: M*N的棋盘,规定其中有K个格子不能放任何东西.(即不能被覆盖) 每一张牌的形状都是1*2,问这个棋盘能否被牌完全覆盖(K个格子除外) 思路: M.N很小,把每一个可以覆盖的格子都离散成一个个 ...

  6. POJ 2446 Chessboard

    要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...

  7. poj 2195 二分图最优匹配 或 最小费用最大流

    就是最基本的二分图最优匹配,将每个人向每个房子建一条边,权值就是他们manhattan距离.然后对所有权值取反,求一次最大二分图最优匹配,在将结果取反就行了. #include<iostream ...

  8. [模板] 匈牙利算法&&二分图最小字典序匹配

    匈牙利算法 简介 匈牙利算法是一种求二分图最大匹配的算法. 时间复杂度: 邻接表/前向星: \(O(n * m)\), 邻接矩阵: \(O(n^3)\). 空间复杂度: 邻接表/前向星: \(O(n ...

  9. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

随机推荐

  1. Android 开发笔记___SD卡文件操作

    package com.example.alimjan.hello_world.Utils; import android.graphics.Bitmap; import android.graphi ...

  2. 节点遍历 element traversal

    任何一个node节点都可以作为遍历的根(traversal root),然后遍历该节点本身以及后代元素,不能遍历祖先或者兄弟元素,方向可以反转   <div id=”div1”> < ...

  3. 通过geotools读写shp文件

    依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...

  4. 4年java开发,该何去何从!

    以前都是一直看,今天楼主第一次发博,还望各位不吝赐教. 今天也没别的要说的,就是吐槽一下楼主这几天的面试遭遇,感觉都有点怀疑人生了. 先说说楼主的情况吧,13年毕业本科计算机专业,到现在一直做java ...

  5. js中的break,continue和return到底怎么用?

    为什么要说个?好像很简单,但是我也会迷糊,不懂有时候为什么要用return,然而break和continue也经常和他放在一起. 所以就一起来说一说,这三个看起来很简单,却常常会出错的关键词的具体用法 ...

  6. C#值参数和引用参数

    一.值参数 未用ref或out修饰符声明的参数为值参数. 使用值参数,通过将实参的值复制到形参的方式,把数据传递到方法.方法被调用时,系统做如下操作. 在栈中为形参分配空间. 复制实参到形参. 值参数 ...

  7. 31.Linux-wm9876声卡驱动(移植+测试)

    本节学习目的 1)分析Linux中的OSS声卡系统 2)移植wm9876声卡 3)使用madplay应用程序播放mp3 1.声音三要素 采样频率 音频采样率是指录音设备在一秒钟内对声音信号的采样次数, ...

  8. js从时间戳中获取日期

    1,从时间戳中解析出年月日时分秒: time为时间戳: var timestr = new Date(parseInt(time) * 1000); var year = timestr.getFul ...

  9. [DFS遍历图]UVA10562 Undraw the Trees

    传送门: 1. UVA - 10562 2. Vjudge [看图写树]     将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...

  10. Session、Cookie 学习笔记

    在开始今天的博文之前首先为自己庆祝一下自己有了三个粉丝,也有了同僚的评论,说实话因为这个开心了好久!哈哈,好了在开始今天的正题之前,首先大家需要了解以下几点: a. HTTP 协议是无状态的协议,WE ...