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; /** * Created by alimjan on 7/5/2017. */ import android.ann ...

  2. [J2EE] 有关 PreparedStatement

    今天同事遇到一个问题,简言之,就是PreparedStatement的预编译究竟是怎么发挥作用的... 嘿嘿,说来惭愧,我以前就只知道PreparedStatement比Statement要好,要防S ...

  3. HTML5对音视频的处理

      前  言 现在网上有许多的框架和插件,能够满足程序猿的各种需求,慢慢的,就有些忽视最基础的东西. 比如,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. H ...

  4. Python 解LeetCode:680. Valid Palindrome II

    题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...

  5. Lucene搜索引擎例子demo

    一.导入相应的jar包 KAnalyzer3.2.0Stable.jar lucene-analyzers-3.0.1.jar lucene-core-3.0.1.jar lucene-highlig ...

  6. 6. ZooKeeper访问控制列表

    ZooKeeper的数据模型提供了ACL机制来控制访问znode. 在创建znode时,ACL将确定你可以在znode上执行的各种操作的权限. ZooKeeper ACL模型与Unix / Linux ...

  7. centos7.2构建Python3.5开发环境

    1.本次使用的是一台全新的腾讯云主机,首先获取linux系统版本信息. [root@VM_46_121_centos ~]# cat /etc/redhat-release <本系统默认自带py ...

  8. [转载] Netty源码分析

    转载自http://blog.csdn.net/kobejayandy/article/details/11836813 Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高 ...

  9. Python之mysql数据库更新表数据接口实现

    昨天,因为项目需求要添加表的更新接口,来存储预测模型训练的数据. 先码为敬~~~~~~~ # -*- coding: utf-8 -*- import pymysql import settings ...

  10. HTML基础--元素类型及类型转换

    元素类型及类型转换 一.XHTML元素分类 根据css显示分类,XHTML元素被分为三种类型:块状元素,内联元素,可变元素 1.块状元素(block element) 1)块状元素在网页中就是以块的形 ...