题意:
也是一个棋盘,规则是“马”不能相互打到。
思路:
奇偶点分开,二分图建图,这道题要注意每个点可以跑八个方向,两边都可以跑,所以边 = 20 * n * n。
然后dinic 要用当前弧优化。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/*-----------------------showtime----------------------*/
const int maxn = ;
int mp[maxn][maxn]; struct E
{
int u,v,val;
int nxt;
}edge[ * maxn*maxn];
int gtot = ,head[maxn*maxn];
void addedge(int u,int v,int val){
edge[gtot].u = u;
edge[gtot].v = v;
edge[gtot].val = val;
edge[gtot].nxt = head[u];
head[u] = gtot++; edge[gtot].u = v;
edge[gtot].v = u;
edge[gtot].val = ;
edge[gtot].nxt = head[v];
head[v] = gtot++;
}
int nx[][] = {
{-,-}, {-,-},{-, },{-,},{,-},{,-},{,},{,}
};
int n,m;
int cal(int i,int j){
return (i-)*n + j;
} int dis[maxn*maxn],cur[maxn*maxn];
bool bfs(int s,int t){
memset(dis, inf, sizeof(dis));
for(int i=s; i<=t; i++) cur[i] = head[i];
queue<int>que;
que.push(s);
dis[s] = ;
while(!que.empty()){
int u = que.front(); que.pop();
for(int i= head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v;
if(edge[i].val > && dis[v] > dis[u] + ){
dis[v] = dis[u] + ;
que.push(v);
}
}
}
return dis[t] < inf;
} int dfs(int u,int t,int maxflow){
if(u == t || maxflow == ) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){
cur[u] = i;
int v = edge[i].v;
if(edge[i].val > && dis[v] == dis[u] + ){
int f = dfs(v, t, min(maxflow, edge[i].val)); if(f > ){
edge[i].val -= f;
edge[i^].val += f;
return f;
}
} }
return ;
}
int dinic(int s,int t){
int flow = ;
while(bfs(s,t)){
while(int f = dfs(s,t,inf)) flow += f;
}
return flow;
}
int main(){
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
int s = , t = n*n+;
int sum = n * n;
for(int i=; i<=m; i++){
int x,y;
scanf("%d%d", &x, &y);
mp[x][y] = ;
sum--;
}
for(int i=; i<=n; i++){
for(int j=; j<=n; j++) {
if((i+j)% == ) {
if(mp[i][j]) addedge(s, cal(i,j), );
else addedge(s, cal(i, j), );
}
else {
if(mp[i][j]) addedge(cal(i,j),t, );
else addedge(cal(i,j), t, );
}
}
} for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
if((i+j)% == ) continue;
for(int k=; k<; k++){
int x = i + nx[k][];
int y = j + nx[k][];
if(x < || x > n || y < || y > n) continue;
addedge(cal(i,j), cal(x,y),inf);
}
}
}
cout<<sum - dinic(s, t)<<endl;
return ;
}