Poj-2446 Chessboard 二分匹配



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 105;
const int M = 50*50;
const int Mod = 1000000007;
int n,m,K;
int nx,ny;
int id[maxn][maxn];
int map[M][M];
int cx[M],cy[M];  
bool vis[M]; 
bool mark[maxn][maxn];
int xs[] = { 0,1,0,-1 };
int ys[] = { 1,0,-1,0 };
int findpath( int u )  
{  
	int i,j;  
	for( i = 1; i <= ny; i++ )  
	{  
		if( map[u][i] && !vis[i] )  
		{  
			vis[i] = 1;  
			if( cy[i] == -1 || findpath( cy[i] ) )  
			{  
				cy[i] = u;  
				cx[u] = i;  
				return true;  
			}  
		}  
	}  
	return false;  
}  

int MaxMatch()  
{  
	int ans = 0;  
	int i,j;  
	memset(cx,-1,sizeof(cx));  
	memset(cy,-1,sizeof(cy));  
	for( i = 1; i <= nx; i ++ )  
	{  
		if( cx[i] == -1 )  
		{  
			memset(vis,0,sizeof(vis));  
			ans += findpath(i);  
		}  
	}  
	return ans;  
}  
int main()
{
	#ifndef ONLINE_JUDGE   
	freopen("data.txt","r",stdin);   
	#endif
	int a,b,xx,yy;
	while( scanf("%d%d%d",&n,&m,&K) != EOF )
	{
		nx = ny = 0;
		for( int i = 0; i < K; i ++ )
		{
			scanf("%d%d",&b,&a);
			mark[a][b] = 1;
		}
		for( int i = 1;i <= n; i ++ )
		{
			for( int j = 1; j <= m; j ++ )
			{
				if( (i+j)%2 == 0 )
					id[i][j] = ++ nx;
				else
					id[i][j] = ++ ny;
			}
		}
		for( int i = 1; i <= n; i ++ )
		{
			for( int j = 1; j <= m; j ++ )
			{
				map[id[i][j]][id[i][j]] = 0;
				if( !mark[i][j] )
					for( int d = 0; d < 4; d ++ )
					{
						xx = i + xs[d];
						yy = j + ys[d];
						if( xx >= 1 && xx <= n && yy >= 1 && yy <= m && !mark[xx][yy] )
						{
							if( (i+j)%2 == 0 )
								map[id[i][j]][id[xx][yy]] = 1;
							else
								map[id[xx][yy]][id[i][j]] = 1;
						}
					}
			}
		}
		int ans = MaxMatch();
		if( nx + ny  - K == ans * 2 )
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值