给一个n*n的棋盘, 上面有障碍物, 有障碍物的不能放东西。然后往上面放马, 马不能互相攻击, 问最多可以放多少个马。
按x+y的奇偶来划分, 如果两个格子可以互相攻击, 就连一条权值为1的边。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {-, -}, {, }, {, -}, {, -}, {, }, {-, -}, {-, } };
const int maxn = 2e5;
int n, a[][];
int q[maxn*], head[], dis[], s, t, num, m;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int check(int x, int y)
{
if(x >= && y >= && x < n && y < n && !a[x][y])
return ;
return ;
}
void solve()
{
init();
s = n*n, t = s+;
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
if(a[i][j])
continue;
if((i+j)%) {
add(s, i*n+j, );
for(int k = ; k < ; k++) {
int x = i+dir[k][];
int y = j+dir[k][];
if(check(x, y)) {
add(i*n+j, x*n+y, );
}
}
} else {
add(i*n+j, t, );
}
}
}
int ans = n*n-dinic()-m;
cout<<ans<<endl;
}
int main()
{
int x, y;
while(~scanf("%d%d", &n, &m)) {
mem(a);
for(int i = ; i < m; i++) {
scanf("%d%d", &x, &y);
x--;
y--;
a[x][y] = ;
}
solve();
}
return ;
}