POJ-2446 Chessboard

#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;

void add(int u, int v) //奇 + 偶
{
Key[top] = v;
}

bool find(int u)
{
int temp;
for(int i = Head[u]; i != -1; i = Next[i])
{
temp = Key[i];
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, false, 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);
}
}
}
}
printf("%s\n", sum(m * n) == ((m * n - k) / 2) ? "YES" : "NO");
}
return 0;
}