Chessboard poj2446(二分最大匹配

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int a[2000][2000]={0};
int aa[2000][2000]={0};//再建一个二分图 
int bb[4][2]={-1,0,0,1,1,0,0,-1};
int pre[2000];
bool vis[2000];
int n,m,p,cnt;
bool dfs(int l){
	for(int i=0;i<cnt;i++){
		if(aa[l][i]==0 || vis[i]==1)
			continue;
		vis[i]=1;
		if(pre[i]==-1 || dfs(pre[i])){
			pre[i]=l;
			return 1;
		}
	}
	return 0;
}
int main(){
	scanf("%d %d %d",&n,&m,&p);
	int b,c;
	for(int i=0;i<p;i++){
		scanf("%d %d",&b,&c);
		a[c][b]=-1;
	}
	if((n*m-p)%2!=0 || n*m==p){
		printf("NO\n");
		return 0;
	}
    int w = 0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(a[i][j]!=-1)
                a[i][j]=w++;//分配标号 
        }
    }
    if(w%2 || w==0)//不为偶数,或没有可以放的地方 
        puts("NO");
    else{
    	//建图
    	cnt=w;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(a[i][j]!=-1){//该点可以建 
					for(int k=0;k<4;k++){
						int xx=i+bb[k][0];
						int yy=j+bb[k][1];
						if(xx<1 || yy<1 || xx>n || yy>m || a[xx][yy]==-1)
							continue;
						aa[a[i][j]][a[xx][yy]]=1;
					}
				}
			}
		}
		int sum=0;
		memset(pre,-1,sizeof(pre));
		for(int i=0;i<cnt;i++){
			memset(vis,0,sizeof(vis));
			if(dfs(i)){
				sum++;
			}
		}
		if(sum==w)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/doublekillyeye/article/details/81392819