【HDU1507】【最大匹配输出路径match数组】

来源:互联网 发布:农村淘宝订单佣金 编辑:程序博客网 时间:2024/06/15 16:26

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2505    Accepted Submission(s): 1034
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 461 11 42 24 14 24 44 344 23 22 23 10 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
 

Recommend
LL




#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;int n,m,k;const int N = 510;int g[N][N];int a[N][N];int b[N*N];int used[N];int match[N];int uN,vN;bool dfs(int u)  {      for(int i=1;i<=vN;i++)      {          if(!used[i] && g[u][i])          {              used[i] = true;              if(match[i] == -1 || dfs(match[i]))              {                  match[i] = u;                  return true;              }          }      }      return false;  }  int hungary()  {      int ret = 0;      for(int i=1;i<=uN;i++)         {          memset(used,0,sizeof(used));          if(dfs(i))  ret++;      }      return ret;  }int main(){    freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    while(scanf("%d%d",&n,&m) ){if(n == 0 && m == 0) break;scanf("%d",&k);int index = 1;memset(a,0,sizeof(a));for(int i=0;i<k;i++){int x,y;scanf("%d%d",&x,&y);x --;y --;a[x][y] = -1;}for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(a[i][j] == -1) continue;b[index] = i * m + j;a[i][j] = index ++;}}memset(g,0,sizeof(g));memset(match,-1,sizeof(match));//cout << "ok" << endl;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(a[i][j] != -1 && (i+j) % 2 == 1){if(i + 1 < n && a[i+1][j] != -1){g[a[i][j]][a[i+1][j]] = 1;}if(i - 1 >= 0 && a[i-1][j] != -1){g[a[i][j]][a[i-1][j]] = 1;}if(j + 1 < m && a[i][j+1] != -1){g[a[i][j]][a[i][j+1]] = 1;}if(j - 1 >= 0 && a[i][j-1] != -1){g[a[i][j]][a[i][j-1]] = 1;}}}}//cout << "ok" << endl;uN = vN = index - 1;int ret = hungary();printf("%d\n",ret);for(int i=1;i<=vN;i++){if(match[i] != -1){int x1 = b[i] / m; int y1 = b[i] % m;int x2 = b[match[i]] / m;int y2 = b[match[i]] % m;printf("(%d,%d)--(%d,%d)\n",x1+1,y1+1,x2+1,y2+1);}}printf("\n");}    return 0;}


0 0
原创粉丝点击