• 欢迎光临~

# 题目链接

## 2199. 骑士共存问题

(1 le n le 200),
(0 le m < n^2)

#### 输入样例：

``````3 2
1 1
3 3
``````

#### 输出样例：

``````5
``````

### 解题思路

• 时间复杂度：(O(n^3))

### 代码

``````// Problem: 骑士共存问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2200/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=205*205,M=N*8*2,inf=1e9;
int n,m,S,T;
bool g[205][205];
int dx[]={-1,-2,-2,-1,1,2,2,1},dy[]={-2,-1,1,2,-2,-1,1,2};
int h[N],f[M],ne[M],e[M],idx;
int d[N],hh,tt,q[N],cur[N];
void add(int a,int b,int c)
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
int get(int x,int y)
{
return (x-1)*n+y;
}
bool bfs()
{
memset(d,-1,sizeof d);
d[S]=hh=tt=0;
q[0]=S;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)return true;
q[++tt]=y;
}
}
}
return false;
}
int dfs(int x,int limit)
{
if(x==T)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
S=0,T=n*n+1;
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x][y]=true;
}
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(g[i][j])continue;
res++;
if((i+j)%2)
{
add(S,get(i,j),1);
for(int k=0;k<8;k++)
{
int x=i+dx[k],y=j+dy[k];
if(x<1||x>n||y<1||y>n||g[x][y])continue;
add(get(i,j),get(x,y),inf);
}
}
else
add(get(i,j),T,1);
}
printf("%d",res-dinic());
return 0;
}
``````