【Luogu】P3355骑士共存问题(最小割)

时间:2022-09-23 18:36:43

  题目链接

  像题面那样把棋盘染成红黄点。发现骑士迈一步能到达的点的颜色一定是跟他所在的格子的颜色不同的。于是(woc哪来的于是?这个性质有这么明显吗?)从源点向所有红点连边,从所有黄点向汇点连边,红点向从它一步能迈到的黄点连边,然后求出最小割(其实就是最大流啦)用可以站骑士的总点数减去。

  网络流构图好喵啊     一脸懵逼

  

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#define maxn 80000
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int n,m;
inline int count(int i){ return i&?i+:i-; }
inline int calc(int x,int y){ return (x-)*n+y; } int u[]={,,,,,-,-,-,-};
int w[]={,-,-,,,,,-,-}; struct Edge{
int next,to,val;
}edge[*maxn];
int head[maxn*],num;
inline void addedge(int from,int to,int val){
edge[++num]=(Edge){head[from],to,val};
head[from]=num;
}
inline void add(int from,int to,int val){
addedge(from,to,val);
addedge(to,from,);
} bool vis[maxn*];
bool ext[][];
int list[maxn*];
int dfn[maxn*];
int Start,End; bool bfs(){
memset(vis,,sizeof(vis));
queue<int> q;
vis[Start]=; dfn[Start]=; q.push(Start);
while(!q.empty()){
int from=q.front();q.pop();
for(int i=head[from];i;i=edge[i].next){
int to=edge[i].to;
if(edge[i].val<=||vis[to]) continue;
vis[to]=;
dfn[to]=dfn[from]+;
q.push(to);
}
}
return vis[End];
} int dfs(int x,int val){
if(val==||x==End) return val;
int flow=; vis[x]=;
for(int &i=list[x];i;i=edge[i].next){
int to=edge[i].to;
if(vis[to]||dfn[to]!=dfn[x]+||edge[i].val==) continue;
int now=dfs(to,min(val,edge[i].val));
val-=now;edge[i].val-=now;edge[count(i)].val+=now;flow+=now;
if(val==) break;
}
if(val!=flow) dfn[x]=-;
return flow;
} int maxflow(){
int ans=;
while(bfs()){
memset(vis,,sizeof(vis));
for(int i=Start;i<=End;++i) list[i]=head[i];
int now=dfs(Start,0x7fffffff);
if(!now) break;
ans+=now;
}
return ans;
} int main(){
n=read(),m=read(); End=n*n+;
for(int i=;i<=m;++i){
int x=read(),y=read();
ext[x][y]=;
}
for(int i=;i<=n;++i)
for(int j=;j<=n;++j){
if(ext[i][j]) continue;
if(!((i+j)&)){
add(Start,calc(i,j),);
for(int k=;k<=;++k){
int x=i+u[k];int y=j+w[k];
if(x<||x>n||y<||y>n) continue;
if(ext[x][y]) continue;
add(calc(i,j),calc(x,y),0x7fffffff);
}
}
else add(calc(i,j),End,);
}
int ans=maxflow();
printf("%d",n*n-m-ans);
}