wanghanjun @ 2019-10-24 21:20:22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN=1000005,MAXM=2000005,INF=10211314;
struct node{
int u,v,c;
node*next,*rev;
}*h[MAXN],pool[MAXM];
int a[205][205],lv[MAXN],q[MAXN],n,m,src,sink,tot=0,inf=291433;
int pos[8][2]={1,2, 2,1, 1,-2, 2,-1, -1,2, -2,1, -1,-2, -2,-1};
void addedge(int u,int v,int c){
// cout<<u<<" "<<v<<" "<<c<<endl;
node*p=&pool[++tot];
node*r=&pool[++tot];
p->u=u;p->v=v;p->c=c;p->next=h[u];h[u]=p;p->rev=r;
r->u=v;r->v=u;r->c=c;r->next=h[v];h[v]=r;r->rev=p;
}
bool bfs(){
memset(lv,-1,sizeof(lv));
int front=0,rear=0;
q[rear++]=src;
lv[src]=0;
while(front<rear){
int u=q[front++];
for(node*p=h[u];p;p=p->next){
if(lv[p->v]<0&&p->c>0){
lv[p->v]=lv[u]+1;
q[rear++]=p->v;
}
}
if(lv[sink]>0){
return 1;
}
}
return lv[sink]>0;
}
int dfs(int u,int key){
if(u==sink){
return key;
}
int res=0;
for(node*p=h[u];p;p=p->next){
if(lv[p->v]==lv[u]+1&&p->c>0){
int tmp=dfs(p->v,min(p->c,key));
if(tmp){
p->c-=tmp;
p->rev->c+=tmp;
res+=tmp;
key-=tmp;
if(key<=0){
break;
}
}
}
}
if(res==0){
lv[u]=-1;
}
return res;
}
int dinic(){
int totflow=0;
while(bfs())
totflow+=dfs(src,INF);
return totflow;
}
int main(){
scanf("%d%d",&n,&m);
src=n*n+1;sink=src+1;
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
a[x][y]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==1){
continue;
}
if((i+j)%2==1){
addedge(src,n*(i-1)+j,1);
for(int k=0;k<8;k++){
int x=pos[k][0]+i,y=pos[k][1]+j;
if(x>=1&&x<=n&&y>=1&&y<=n){
addedge(n*(i-1)+j,n*(x-1)+y,inf);
}
}
}
else{
addedge(n*(i-1)+j,sink,1);
}
}
}
printf("%d\n",n*n-m-dinic());
return 0;
}
AC*4
WA*8
by wanghanjun @ 2019-10-24 21:20:48
是WA*7,打错了