首页 > 代码库 > XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*
XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*
B. Uncle Tom‘s Inherited Land*
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d Java class name: MainSpecial Judge
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).
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)
解题:根据奇偶性建图+最大匹配数+最大匹配数的记录。根据奇偶性建图:在一个平面坐标系中,某个格子的 纵横坐标和 与其相领格子的 纵横坐标和 的绝对值相差1,即相差一个数。由于奇数与偶数相间分布,故只选择纵横坐标和为奇数或者纵横坐标和为偶数的点进行建图(从始至终只能选择其中一种方式),这样保证了是一个二分图。
这题的写法,把原先的匈牙利算法的线段的端点的由的写成了二维的,算法还是那样的。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 const int maxn = 110;16 struct{17 int x,y;18 }link[maxn][maxn];19 int n,m;20 bool mp[maxn][maxn],vis[maxn][maxn];21 bool isIn(int x,int y){22 if(x < 1 || x > n || y < 1 || y > m)23 return false;24 return true;25 }26 bool dfs(int x,int y){27 static const int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};28 for(int i = 0; i < 4; i++){29 int tx = x+dir[i][0];30 int ty = y+dir[i][1];31 if(isIn(tx,ty) && !vis[tx][ty] && !mp[tx][ty]){32 vis[tx][ty] = true;33 if(link[tx][ty].x == -1 || dfs(link[tx][ty].x,link[tx][ty].y)){34 link[tx][ty].x = x;35 link[tx][ty].y = y;36 return true;37 }38 }39 }40 return false;41 }42 int main(){43 int i,j,u,v,k;44 while(scanf("%d%d",&n,&m),n+m){45 scanf("%d",&k);46 memset(mp,false,sizeof(mp));47 while(k--){48 scanf("%d%d",&u,&v);49 mp[u][v] = 1;50 }51 int ans = 0;52 memset(link,-1,sizeof(link));53 for(i = 1; i <= n; i++){54 for(j = 1; j <= m; j++){55 memset(vis,false,sizeof(vis));56 if(((i+j)&1) && !mp[i][j] && dfs(i,j)) ans++;57 }58 }59 printf("%d\n",ans);60 for(i = 1; i <= n; i++){61 for(j = 1; j <= m; j++){62 if(link[i][j].x != -1)63 printf("(%d,%d)--(%d,%d)\n",link[i][j].x,link[i][j].y,i,j);64 }65 }66 printf("\n");67 }68 return 0;69 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。