您的位置:首页 > 其它

Poj-2446 Chessboard 二分匹配

2014-07-14 14:42 393 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: