HDU Uncle Toms Inherited Land 二分图匹配

 

Uncle Tom's Inherited Land*

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 5
Special Judge
Problem Description
Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle's property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks). 
 

 

Input
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.
 

 

Output
For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.
 

 

Sample Input
4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
4 3
4
4 2
3 2
2 2
3 1
0 0
 

 

Sample Output
4
(1,2)--(1,3)
(2,1)--(3,1)
(2,3)--(3,3)
(2,4)--(3,4)

3
(1,1)--(2,1)
(1,2)--(1,3)
(2,3)--(3,3)

 

 

Source
South America 2002 - Practice
 
78ms 1Y,题目给定了一个N*M的方阵,用1*2的方块去覆盖,约束条件是有些特定块不能被覆盖,问这种约束条件下最大可以放几块1*2的方块。
对N*M的进行黑白染色,黑色为X集合,白色为Y集合。如果黑色和白色相邻的话从黑色块向白色块连边。最后求最大匹配即可。
这个题目建图稍微有点恶心啊~ 
View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 using namespace std;
  6 #define maxn 101
  7 typedef struct edge{
  8     int v;
  9     edge *next;
 10 }edge;
 11 edge map[maxn],e[20000];
 12 int mat[maxn][maxn];
 13 int id[maxn][maxn];
 14 int cnt,n,m,kk,nx,ny;
 15 int dxx[]={-1,0,1,0},dyy[]={0,1,0,-1};
 16 int xx[maxn],xy[maxn],yx[maxn],yy[maxn];
 17 void addedge(int u,int v){
 18     edge *p=e+cnt++;
 19     p->v=v; p->next=map[u].next; map[u].next=p;;
 20 }
 21 typedef struct node{
 22     int x,y,c,id;
 23     node(int xx,int yy,int cc,int idd){x=xx;y=yy;c=cc;id=idd;}
 24     node(){}
 25 }node;
 26 int mx[maxn],my[maxn],dx[maxn],dy[maxn];
 27 void init(){
 28     memset(map,0,sizeof(map));
 29     memset(mat,-1,sizeof(mat));
 30     cnt=nx=ny=0;
 31     scanf("%d",&kk);
 32     int a,b;
 33     for(int i=0;i<kk;i++){
 34         scanf("%d%d",&a,&b);
 35         mat[a][b]=0;
 36     }nx=0;ny=0;queue<node>q;
 37     for(int i=1;i<=n;i++){
 38         for(int j=1;j<=m;j++){
 39             if(mat[i][j]==-1){
 40                 mat[i][j]=1;
 41                 q.push(node(i,j,1,nx));id[i][j]=nx;xx[nx]=i;xy[nx]=j;nx++;
 42                 while(!q.empty()){
 43                     node u=q.front();q.pop();
 44                     for(int k=0;k<4;k++){
 45                         int tx=u.x+dxx[k],ty=u.y+dyy[k];
 46                         if(tx>=1 && ty>=1 && tx<=n && ty<=m && mat[tx][ty]==-1){
 47                             mat[tx][ty]=3-u.c;
 48                             if(u.c==1){
 49                                 q.push(node(tx,ty,2,ny));id[tx][ty]=ny;yx[ny]=tx;yy[ny]=ty;ny++;
 50                             }else{
 51                                 q.push(node(tx,ty,1,nx));id[tx][ty]=nx;xx[nx]=tx;xy[nx]=ty;nx++;
 52                             }
 53                         }
 54                     }
 55                 }
 56             }
 57         }
 58     }
 59     for(int i=1;i<=n;i++){
 60         for(int j=1;j<=m;j++){
 61             if(mat[i][j]==1){
 62                 for(int k=0;k<4;k++){
 63                     int tx=i+dxx[k],ty=j+dyy[k];
 64                     if(tx>=1 && ty>=1 && tx<=n && ty<=m && mat[tx][ty]==2){
 65                         addedge(id[i][j],id[tx][ty]);
 66                     }
 67                 }
 68             }
 69         }
 70     }
 71 }
 72 bool bfs(){
 73     queue<int >q;
 74     memset(dx,0,sizeof(dx));
 75     memset(dy,0,sizeof(dy));
 76     for(int i=0;i<nx;i++)if(mx[i]==-1) q.push(i);
 77     bool flag=false;
 78     while(!q.empty()){
 79         int u=q.front();q.pop();
 80         for(edge *e=map[u].next;e;e=e->next){
 81             int v=e->v;
 82             if(!dy[v]){
 83                 dy[v]=dx[u]+1;
 84                 if(my[v]==-1) flag=true;
 85                 else {dx[my[v]]=dy[v]+1; q.push(my[v]);}
 86             }
 87         }
 88     }
 89     return flag;
 90 }
 91 bool dfs(int u){
 92     for(edge*e=map[u].next;e;e=e->next){
 93         int v=e->v;
 94         if(dy[v]==dx[u]+1){
 95             dy[v]=0;
 96             if(my[v]==-1 || dfs(my[v])){
 97                 my[v]=u; mx[u]=v;
 98                 return true;
 99             }
100         }
101     }
102     return false;
103 }
104 int hk(){
105     int ans=0;
106     memset(mx,-1,sizeof(mx));
107     memset(my,-1,sizeof(my));
108     while(bfs()){
109         for(int i=0;i<nx;i++){
110             if(mx[i]==-1 && dfs(i)) ans++;
111         }
112     }
113     return ans;
114 }
115 int main()
116 {
117     freopen("in.txt","r",stdin);
118     while(scanf("%d%d",&n,&m)!=EOF){
119         if(n==0 && m==0) break;
120         init();
121         printf("%d\n",hk());
122         for(int i=0;i<nx;i++){
123             if(mx[i]!=-1){
124                 printf("(%d,%d)--(%d,%d)\n",xx[i],xy[i],yx[mx[i]],yy[mx[i]]);
125             }
126         }
127         printf("\n");
128     }
129     return 0;
130 }

 

posted @ 2012-04-24 22:16  lmnx  阅读(182)  评论(0编辑  收藏  举报