HDOJ 1507 – Uncle Tom’s Inherited Land* 構圖做二分圖最大匹配

來源:互聯網
上載者:User

                 題意:

                         要在一個矩陣中放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 105using 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.