(网络流24题大多需要spj,所以需要一个有spj的oj,本系列代码均在www.oj.swust.edu.cn测试通过)
这道题一看到不能共存就知道肯定是最小割了,然后就是怎么建图的问题了,题目良心,给出了一个图,这个图已经染了色,我们可以清楚地看到按照图中的方式染色的话,一个骑士能攻击到的其他位置必定和其所在点的颜色不同,所以按照这个图的方式染色,若左面的点会攻击到右面的点,则向能攻击到的点都链接一条INF,跑一边最小割就好啦。
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<string>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 100000000
struct bian
{
int l,r,f;
}a[1000000];
int nex[1000000];
int fir[1000000];
int tot=1;
void add_edge(int l,int r,int f)
{
a[++tot].l=l;
a[tot].r=r;
a[tot].f=f;
nex[tot]=fir[l];
fir[l]=tot;
}
int d[100000];
int s=0,t=99999;
bool bfs()
{
static int dui[1000000];
memset(d,-1,sizeof(d));
int top=1,my_final=2;
dui[1]=s;
d[s]=1;
while(top<my_final)
{
int u=dui[top++];
for(int o=fir[u];o;o=nex[o])
{
if(d[a[o].r]==-1 && a[o].f)
{
d[a[o].r]=d[u]+1;
dui[my_final++]=a[o].r;
if(a[o].r==t) return true;
}
}
}
return false;
}
int dinic(int u,int flow)
{
if(u==t) return flow;
int left=flow;
for(int o=fir[u];o&&left;o=nex[o])
{
if(a[o].f && d[a[o].r]==d[u]+1)
{
int temp=dinic(a[o].r,min(left,a[o].f));
if(!temp) d[a[o].r]=-1;
left-=temp;
a[o].f-=temp;
a[o^1].f+=temp;
}
}
return flow-left;
}
int n,m;
bool mapp[1000][1000];
int xx[8]={-2,-2,-1,-1,1,1,2,2};
int yy[8]={-1,1,-2,2,-2,2,-1,1};
inline int wz(int x,int y)
{
return (x-1)*n+y;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mapp[x][y]=true;
}
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(!mapp[i][j])
{
ans++;
if(((i+j)&1))
{
add_edge(s,wz(i,j),1);
add_edge(wz(i,j),s,0);
for(int k=0;k<8;k++)
{
int nx=i+xx[k];
int ny=j+yy[k];
if(!mapp[nx][ny] && nx>=1 && nx<=n && ny>=1 && ny<=n)
{
add_edge(wz(i,j),wz(nx,ny),INF);
add_edge(wz(nx,ny),wz(i,j),0);
}
}
}
else
{
add_edge(wz(i,j),t,1);
add_edge(t,wz(i,j),0);
}
}
}
while(bfs()) ans-=dinic(s,INF);
cout<<ans;
return 0;
}