HDU1507--Uncle Tom's Inherited Land*
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1249 Accepted Submission(s): 544
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)
黑白染色+最大匹配
奇数做A集合,偶数做B集合
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 8 struct node 9 { 10 int x,y; 11 }id[5001]; 12 13 int g[1001][1001]; 14 int Link[1001][1001]; 15 int num[1001][1001]; 16 int cy[1001]; 17 int mk[1001]; 18 int n,m; 19 int cnt; 20 21 int path(int u) 22 { 23 int v; 24 for(v=1;v<cnt;v++) 25 { 26 if((id[v].x+id[v].y)%2==1)continue; 27 if(!mk[v]&&num[u][v]) 28 { 29 mk[v]=1; 30 if(cy[v]==-1||path(cy[v])) 31 { 32 cy[v]=u; 33 return 1; 34 } 35 } 36 } 37 return 0; 38 } 39 40 int maxmatch() 41 { 42 int i; 43 int sum=0; 44 memset(cy,0xff,sizeof(cy)); 45 for(i=1;i<cnt;i++) 46 { 47 if((id[i].x+id[i].y)%2==0)continue; 48 memset(mk,0,sizeof(mk)); 49 sum+=path(i); 50 } 51 return sum; 52 } 53 54 int main() 55 { 56 while(scanf("%d%d",&n,&m)!=EOF) 57 { 58 int i,j; 59 if(!n&&!m)break; 60 int t; 61 scanf("%d",&t); 62 memset(g,0xff,sizeof(g)); 63 memset(num,0,sizeof(num)); 64 memset(Link,0,sizeof(Link)); 65 while(t--) 66 { 67 int s,e; 68 scanf("%d%d",&s,&e); 69 g[s][e]=0; 70 } 71 cnt=1; 72 for(i=1;i<=n;i++) 73 { 74 for(j=1;j<=m;j++) 75 { 76 if(g[i][j]==-1) 77 { 78 id[cnt].x=i,id[cnt].y=j; 79 Link[i][j]=cnt++; 80 } 81 } 82 } 83 for(i=1;i<=n;i++) 84 { 85 for(j=1;j<=m;j++) 86 { 87 if(g[i][j]==-1) 88 { 89 if(i+1<=n&&g[i+1][j]==-1) 90 { 91 num[Link[i][j]][Link[i+1][j]]=num[Link[i+1][j]][Link[i][j]]=1; 92 } 93 if(j+1<=m&&g[i][j+1]==-1) 94 { 95 num[Link[i][j]][Link[i][j+1]]=num[Link[i][j+1]][Link[i][j]]=1; 96 } 97 if(i-1>=1&&g[i-1][j]==-1) 98 { 99 num[Link[i][j]][Link[i-1][j]]=num[Link[i-1][j]][Link[i][j]]=1; 100 } 101 if(j-1>=1&&g[i][j-1]==-1) 102 { 103 num[Link[i][j]][Link[i][j-1]]=num[Link[i][j-1]][Link[i][j]]=1; 104 } 105 } 106 } 107 } 108 int ans=maxmatch(); 109 printf("%d\n",ans); 110 for(i=1;i<cnt;i++) 111 { 112 if(cy[i]>0) 113 { 114 printf("(%d,%d)--(%d,%d)\n",id[i].x,id[i].y,id[cy[i]].x,id[cy[i]].y); 115 } 116 } 117 printf("\n"); 118 } 119 return 0; 120 }