# POJ - 2446 Chessboard （二分图匹配）

(0,0) (0,1)(0,2)(0,3)

(1,0) (1,1)(1,2)(1,3)

(2,0) (2,1)(2,2)(2,3)

(3,0) (3,1)(3,2)(3,3)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int M = 33 * 33, N = M * M;
int match[M];
bool use[M],map[M][M];
int m, n, k;
struct node {
int v;
int next;
}edge[N];
void add(int u, int v) //奇 + 偶
{
edge[top].v = v;
}
bool  find(int u){
int temp;
for(int i = Head[u] ; i != - 1; i = edge[i].next){
temp = edge[i].v;
if(!use[temp]){
use[temp] = true;
if(match[temp] == -1 || find(match[temp])){
match[temp] = u;
return true;
}
}
}
return false;
}
int sum(int n){
int sumall = 0;
for(int i = 0 ; i < n ; i ++){
memset(use,0,sizeof(use));
if(find(i))
sumall++;
}
return sumall;
}

int main()
{
int x, y;
while(scanf("%d%d%d", &m, &n, &k) != EOF)
{
if((m * n - k) & 1)
{
printf("NO\n");
continue;
}
top = 0;
memset(match, -1, sizeof(match));
memset(map, true, sizeof(map));
for(int i = 0; i < k; ++i)
{
scanf("%d%d", &y, &x); //先y后x
map[x - 1][y - 1] = false;
}
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(map[i][j])
{
if(i - 1 >= 0 && map[i - 1][j]) //上
{
if((i + j) & 1)
add(i * n + j, (i - 1) * n + j);
else
add((i - 1) * n + j, i * n + j);
}
if(j - 1 >= 0 && map[i][j - 1]) //左
{
if((i + j) & 1)
add(i * n + j, i * n + (j - 1));
else
add( i * n +(j - 1) , i * n + j);
}
}
}
}
for(int i = 0 ; i < n * m ; i ++){
for(int k = Head[i] ; k != -1 ; k = edge[k].next)
cout << i << " " << edge[i].v << endl;
}
printf("%s\n", sum(m * n) == ((m * n - k) / 2) ? "YES" : "NO");
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120