棋盘覆盖
分析
见《进阶指南》第426
页。
实现
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110, M = 10010;
struct edge {
int to, next;
};
edge e[4 * M];
int idx, head[M];
int n, m;
int a[N][N];
int ln, rn;
bool mark[M];
int match[M];
void add_edge (int u, int v) {
e[idx].to = v;
e[idx].next = head[u];
head[u] = idx ++;
}
bool dfs (int cur) {
for (int i = head[cur]; i != -1; i = e[i].next) {
int to = e[i].to;
if (mark[to] == true) continue;
mark[to] = true;
if (match[to] == 0 || dfs(match[to]) == true) {
match[to] = cur;
return true;
}
}
return false;
}
int main () {
memset(head, -1, sizeof(head));
cin >> n >> m;
for (int i = 1, x, y; i <= m; ++ i) {
cin >> x >> y;
a[x][y] = -1;
}
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= n; ++ j)
if ((i + j) % 2 == 0 && a[i][j] != -1)
a[i][j] = ++ ln;
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= n; ++ j)
if ((i + j) % 2 == 1 && a[i][j] != -1)
a[i][j] = ln + (++ rn);
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) {
if (a[i][j] == -1) continue;
if (j + 1 <= n && a[i][j + 1] != -1) {
add_edge(a[i][j], a[i][j + 1]);
add_edge(a[i][j + 1], a[i][j]);
}
if (i + 1 <= n && a[i + 1][j] != -1) {
add_edge(a[i][j], a[i + 1][j]);
add_edge(a[i + 1][j], a[i][j]);
}
}
}
int res = 0;
for (int i = 1; i <= ln; ++ i) {
memset(mark, 0, sizeof(mark));
if (dfs(i) == true) ++ res;
}
cout << res << endl;
return 0;
}