易错点:
- 可以双向加边这个一定要赞一个.
- 在这种一次匹配则两个对象都被对方匹配上的题目中,应当在dfs成功时设置两个对象的match值为对方,即match[u]=v,match[v]=u.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=20000;
struct Edge{
int from,to,nxt;
}e[MAXN*100];
int head[MAXN*100],edgeCnt=1;
void addEdge(int u,int v){
e[++edgeCnt].from=u;
e[edgeCnt].to=v;
e[edgeCnt].nxt=head[u];
head[u]=edgeCnt;
}
bool vis[MAXN];
int match[MAXN];
bool dfs(int x){
for(int i=head[x];i;i=e[i].nxt){
int nowV=e[i].to;
if(vis[nowV])continue;
vis[nowV]=1;
if(!match[nowV]||dfs(match[nowV])){
match[nowV]=x;
match[x]=nowV;
return 1;
}
}
return 0;
}
int n;
int getHash(int i,int j){
return (i-1)*n+j;
}
bool ban[110][110];
int main(){
int t;
scanf("%d%d",&n,&t);
for(int i=1;i<=t;i++){
int x,y;
scanf("%d%d",&x,&y);
ban[x][y]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(ban[i][j])continue;
int nowV=getHash(i,j);
if(!ban[i-1][j]&&i-1>0)addEdge(nowV,getHash(i-1,j));
if(!ban[i+1][j]&&i+1<=n)addEdge(nowV,getHash(i+1,j));
if(!ban[i][j+1]&&j+1<=n)addEdge(nowV,getHash(i,j+1));
if(!ban[i][j-1]&&j-1>0)addEdge(nowV,getHash(i,j-1));
}
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
int nowV=getHash(i,j);
if(!match[nowV]){
memset(vis,0,sizeof(vis));
if(dfs(nowV))ans++;
}
}
printf("%d\n",ans);
return 0;
}