码迷,mamicode.com
首页 > 其他好文 > 详细

棋盘覆盖TYVJ1035(二分图最大匹配)

时间:2018-04-05 22:32:48      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:log   continue   class   print   tyvj   ++   gpo   for   ret   

#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;
}

 

棋盘覆盖TYVJ1035(二分图最大匹配)

标签:log   continue   class   print   tyvj   ++   gpo   for   ret   

原文地址:https://www.cnblogs.com/lmjer/p/8724539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!