hdu 1507(二分图匹配)
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3232 Accepted Submission(s): 1354
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).
![](http://acm.hdu.edu.cn/data/images/1507-1.gif)
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).
![](http://acm.hdu.edu.cn/data/images/1507-1.gif)
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
题意:在一个矩阵里面找到最多的1*2的矩形填这个矩阵的白色区域。
题解:这个题可以利用棋盘染色法将其化成二分图,因为二分图的X一个点只能与Y的一个点进行匹配,所以这样的话就避免了重复计数。然后根据linker数组找到相邻端点,取模输出就好,输出时要讨论 y 是否为 m ,不然的话会取模变成 0。
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> using namespace std; const int N = 10005; struct Edge{ int v,next; }edge[200000]; int head[N],tot; int n,m; int mp[105][105]; int linker[N]; bool vis[N]; int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}}; void addEdge(int u,int v,int &k){ edge[k].v = v,edge[k].next = head[u],head[u] = k++; } void init(){ memset(head,-1,sizeof(head)); tot = 0; } int P(int x,int y){ return (x-1)*m+y; } bool check(int x,int y){ if(x<1||x>n||y<1||y>m||mp[x][y]) return false; return true; } bool dfs(int u){ for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v; if(!vis[v]){ vis[v] = true; if(linker[v]==-1||dfs(linker[v])){ linker[v] = u; return true; } } } return false; } int main() { while(scanf("%d%d",&n,&m)!=EOF,n+m){ init(); memset(mp,0,sizeof(mp)); int a,b,k; scanf("%d",&k); while(k--){ scanf("%d%d",&a,&b); mp[a][b] = 1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(!mp[i][j]&&(i+j)%2){ for(int k=0;k<4;k++){ int x = i + dir[k][0]; int y = j + dir[k][1]; if(check(x,y)){ addEdge(P(i,j),P(x,y),tot); } } } } } memset(linker,-1,sizeof(linker)); int res = 0; for(int i=1;i<=n*m;i++){ memset(vis,false,sizeof(vis)); if(dfs(i)) res++; } printf("%d\n",res); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int x1,y1,x2,y2; int now = P(i,j); if(linker[now]!=-1){ if(linker[now]%m==0){ x1 = linker[now]/m; y1 = m; }else{ x1 = linker[now]/m+1; y1 = linker[now]%m; } if(now%m==0){ x2 = now/m; y2 = m; }else{ x2 = now/m+1; y2 = now%m; } printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2); } } } printf("\n"); } return 0; }