棋盘覆盖TYVJ1035(二分图最大匹配)
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=11000; struct my{ int next; int v; }; bool biao[110][110]; bool vis[maxn]; int match[maxn]; int n,m,adj[maxn],fa; my bian[maxn*4]; void myinsert(int u,int v){ bian[++fa].v=v; bian[fa].next=adj[u]; adj[u]=fa; } int getnum(int i,int j){ return (i-1)*n+j; } bool dfs(int x){ for (int i=adj[x];i;i=bian[i].next){ int v=bian[i].v; if(!vis[v]){ vis[v]=true; if(!match[v]||dfs(match[v])){ match[v]=x; return true; } } } return false; } int main(){ scanf("%d%d",&n,&m); int u,v; for (int i=1;i<=m;i++){ scanf("%d%d",&u,&v); biao[u][v]=true; } for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ if(biao[i][j] || (i+j) % 2 == 0) continue; if(j > 1 && !biao[i][j-1]) myinsert(getnum(i,j), getnum(i,j-1)),myinsert(getnum(i,j-1), getnum(i,j)); if(i > 1 && !biao[i-1][j]) myinsert(getnum(i,j), getnum(i-1,j)),myinsert(getnum(i-1,j), getnum(i,j)); if(j < n && !biao[i][j+1]) myinsert(getnum(i,j), getnum(i,j+1)),myinsert(getnum(i,j+1), getnum(i,j)); if(i < n && !biao[i+1][j]) myinsert(getnum(i,j), getnum(i+1,j)),myinsert(getnum(i+1,j), getnum(i,j)); } int ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){ memset(vis,0,sizeof(vis)); if(dfs(getnum(i,j))) ans++; } printf("%d\n",ans/2); return 0; }