HDOJ 1507 - Uncle Tom's Inherited Land* 构图做二分图最大匹配

                 题意:

                         要在一个矩阵中放1*2的长方形..有些点不能放长方形.问最多可以放多少个....

                 题解:

                        这题我第一反应是状态压缩DP..但是看范围.好吧..二分图匹配..但是有个问题..如果直接的点对点的做边..会出现混乱..并且不符合2分图的基本模型(同侧的点无直接的任何关系才行)...所以做二分图..第一件事就是把点分成内部不会直接影响的两堆..本题以(x+y)为奇数或偶数来分成两堆..剩下的就很裸了..


Program:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#define ll long long
#define MAXN 105
using namespace std;
int n,match[MAXN],hash[MAXN][MAXN],w[MAXN*MAXN][2];
bool arc[MAXN][MAXN],used[MAXN],s[MAXN][MAXN]; 
bool dfs(int x)
{
       int i;
       for (i=1;i<=n;i++)
          if (arc[x][i] && !used[i])
          {
                 used[i]=true;
                 if (!match[i] || dfs(match[i]))
                 {
                       match[i]=x;
                       return true;
                 }
          }       
       return false; 
}
int getmax()
{
       int sum=0;
       memset(match,0,sizeof(match));
       for (int i=1;i<=n;i++)
       {
               memset(used,false,sizeof(used));
               sum+=dfs(i);
       }
       return sum;
}
int main()
{
       int i,j,x,y,m,num,cases=0;  
       while (~scanf("%d%d",&n,&m) && n)
       { 
                memset(s,true,sizeof(s));
                scanf("%d",&num);
                while (num--) { scanf("%d%d",&x,&y),s[x][y]=false; };
                num=0;
                memset(hash,0,sizeof(hash));
                for (x=1;x<=n;x++)
                  for (y=1;y<=m;y++)
                     if (s[x][y]) hash[x][y]=++num,w[num][0]=x,w[num][1]=y;
                memset(arc,false,sizeof(arc));
                for (x=1;x<=n;x++)
                   for (y=1;y<=m;y++)
                      if ((x+y)%2 && s[x][y])
                      {
                             if (x!=1 && s[x-1][y]) arc[hash[x][y]][hash[x-1][y]]=true;
                             if (y!=1 && s[x][y-1]) arc[hash[x][y]][hash[x][y-1]]=true;
                             if (x!=n && s[x+1][y]) arc[hash[x][y]][hash[x+1][y]]=true;
                             if (y!=m && s[x][y+1]) arc[hash[x][y]][hash[x][y+1]]=true;
                      }
                n=num;
                if (cases) printf("\n");
                cases++;
                printf("%d\n",getmax());
                for (i=1;i<=n;i++)
                  if (match[i]) 
                     printf("(%d,%d)--(%d,%d)\n",w[match[i]][0],w[match[i]][1],w[i][0],w[i][1]);
       }
       return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值