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

来源:互联网 发布:2016年7月非农数据 编辑:程序博客网 时间:2024/06/06 04:47

题意:给出一个矩形N*M棋盘,有K个格子是空洞,然后用2*1的矩形,对所有非空洞的格子进行覆盖,如果可以全部覆盖,就输出YES;

思路:和HDU1507差不多,而且还简单点不用输出组合,见代码


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=1000+5;struct Max_Match{    int n,m;    vector<int> g[maxn];    bool vis[maxn];    int left[maxn];    void init(int n,int m)    {        this->n=n;this->m=m;        for(int i=1; i<=n; ++i) g[i].clear();        memset(left,-1,sizeof(left));    }    bool match(int u)    {        for(int i=0;i<g[u].size();++i)        {            int v=g[u][i];            if(!vis[v])            {                vis[v]=true;                if(left[v]==-1 || match(left[v]))                {                    left[v]=u;                    return true;                }            }        }        return false;    }    int solve()    {        int ans=0;        for(int i=1; i<=n; ++i)        {            memset(vis,0,sizeof(vis));            if(match(i)) ++ans;        }        return ans;    }}MM;int cas=1;int mapp[maxn][maxn];int n,m;struct Node{int x,y;Node(){}Node(int x,int y):x(x),y(y){}}node1[maxn],node2[maxn];bool check(int i,int j){if (node1[i].x+1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x-1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y+1==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y-1==node2[j].y)return true;return false;}int main(){int k;    while(scanf("%d%d%d",&n,&m,&k)!=EOF && n)    {int kk = k;memset(mapp,0,sizeof(mapp)); while (k--){int r,c;scanf("%d%d",&r,&c);mapp[c][r]=-1;}int num1=0,num2=0;        for (int i = 1;i<=n;i++)for (int j = 1;j<=m;j++)if (mapp[i][j]==0){if ((i+j)%2==0)node1[++num1]=Node(i,j);elsenode2[++num2]=Node(i,j);}MM.init(num1,num2);for (int i = 1;i<=num1;i++)for (int j = 1;j<=num2;j++)                if (check(i,j)){MM.g[i].push_back(j);}     //   printf("%d\n",MM.solve());//printf("%d %d %d\n",n,m,kk);printf("%s\n",(MM.solve()*2 == (n*m-kk)) ?"YES":"NO");    }    return 0;}

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 22 13 3

Sample Output

YES

Hint


A possible solution for the sample input.



0 0
原创粉丝点击