POJ 2446 Chessboard 二分图

来源:互联网 发布:域名找回 编辑:程序博客网 时间:2024/06/08 13:07

Chessboard

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19313 Accepted: 6093

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.
Source

POJ Monthly,charlescpp

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

题解:

傻逼题。傻逼题!傻逼题!!!

注意输入的每个x,y,其实是代表y行第x列。
我调了辣么久,曾经一度怀疑人生:)

对于这种方格、矩形的二分图,尤其又涉及到相邻问题的,经常按奇偶来建图,然后相邻点连边。跑二分图最大匹配,如果最大匹配数*2+洞的数量=n*m,就可以,否则不行。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int NN = 50;const int N = 2024 + 10;const int M = 200000 + 10;int n,m,k;int mp[NN][NN];struct node{    int pre,v;}e[M];int num=0,head[N];void addedge(int from,int to){    e[++num].pre=head[from],e[num].v=to;    head[from]=num;}bool vis[N];int c[N];bool find(int u){    for(int i=head[u];i;i=e[i].pre){        int v=e[i].v;        if(!vis[v]){          vis[v]=true;          if(c[v]==-1||find(c[v])){             c[v]=u;             return true;          }        }    }    return false;}int getans(){    int ans=0;    memset(c,-1,sizeof(c));    for(int i=1;i<=n;++i){        for(int j=1;j<=m;++j){            if(!mp[i][j]&&((i+j)&1)==0){                memset(vis,0,sizeof(vis));                int u=(i-1)*m+j;                if(find(u)) ++ans;            }        }    }    return ans;}#define ms(x,y) memset(x,y,sizeof(x))void update(){    num=0,ms(head,0),ms(mp,0);}int main(){    scanf("%d%d%d",&n,&m,&k);    update();    for(int i=1;i<=k;++i){        int x,y;scanf("%d%d",&x,&y);        mp[y][x]=1;    }    for(int i=1;i<=n;++i){        for(int j=1;j<=m;++j){            if(!mp[i][j]&&((i+j)&1)==0){                if(i>1&&!mp[i-1][j]) addedge((i-1)*m+j,(i-2)*m+j);                if(j>1&&!mp[i][j-1]) addedge((i-1)*m+j,(i-1)*m+j-1);                if(i<n&&!mp[i+1][j]) addedge((i-1)*m+j,i*m+j);                if(j<m&&!mp[i][j+1]) addedge((i-1)*m+j,(i-1)*m+j+1);            }        }    }    int ans=getans();    if((ans*2+k)==n*m) printf("YES\n");    else printf("NO\n");    return 0;}