hdu1507Uncle 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): 1943 Accepted Submission(s): 809

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

这题输出只要是合理都可以。

#include<stdio.h>
#include<string.h>
int map[5005][4],vist[5005],match[5005];
int find(int i)
{
    for(int j=0;j<4;j++)
    if(!vist[map[i][j]]&&map[i][j])
    {
        vist[map[i][j]]=1;
        if(match[map[i][j]]==0||find(match[map[i][j]]))
        {
            match[map[i][j]]=i; return 1;
        }
    }
    return 0;
}
int main()
{
    int n,m,mp[105][105],k,a,b,ans,x[2][5005],y[2][5005];
    while(scanf("%d%d",&n,&m)>0&&m+n!=0)
    {
        scanf("%d",&k);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        mp[i][j]=1;
        while(k--)
        {
            scanf("%d%d",&a,&b); mp[a][b]=0;
        }
        int wn=0,bn=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        if((i+j)%2&&mp[i][j])
        {
            bn++; mp[i][j]=bn; x[1][bn]=i; y[1][bn]=j;
        }
        else if(mp[i][j])
        {
            wn++; mp[i][j]=wn; x[0][wn]=i; y[0][wn]=j;
        }
        for(int i=1;i<=bn;i++)
             for(int j=0;j<4;j++)
                map[i][j]=0;
       for(int i=1;i<=wn;i++)
            match[i]=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        if(mp[i][j]&&(i+j)%2)
        {
            if(i-1>0&&mp[i-1][j])map[mp[i][j]][2]=mp[i-1][j];
            if(j-1>0&&mp[i][j-1])map[mp[i][j]][0]=mp[i][j-1];
            if(j+1<=m&&mp[i][j+1])map[mp[i][j]][1]=mp[i][j+1];
            if(i+1<=n&&mp[i+1][j])map[mp[i][j]][3]=mp[i+1][j];
        }
        ans=0;
        for(int i=1;i<=bn;i++)
        {
            for(int j=1;j<=wn;j++)
                vist[j]=0;
            ans+=find(i);
        }
        printf("%d\n",ans);
        for(int i=1;i<=wn;i++)
        if(match[i])
        {
            int x1,y1,x2,y2;
            x1=x[0][i]; y1=y[0][i];
            x2=x[1][match[i]]; y2=y[1][match[i]];
            if(x1>x2||x1==x2&&y1>y2)
            printf("(%d,%d)--(%d,%d)\n",x2,y2,x1,y1);
            else
            printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2);
        }
        printf("\n");
    }
}

hdu1507Uncle Tom's Inherited Land*(最大匹配,黑白染色),布布扣,bubuko.com

hdu1507Uncle Tom's Inherited Land*(最大匹配,黑白染色)

时间: 2024-08-06 03:45:22

hdu1507Uncle Tom's Inherited Land*(最大匹配,黑白染色)的相关文章

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

ZOJ1516 Uncle Tom&#39;s Inherited Land(二分图最大匹配)

一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个二分图的最大边独立集,也就是最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 #define

ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 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-uncl

hdu Uncle Tom&#39;s Inherited Land*(1*2矩阵覆盖,最大匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=1507 大致题意:在一个n*m的格子上,黑色的地方不可用,问在白色格子上最多可放多少1*2的矩阵. 思路:建图,每个白色格子与它临近的上下左右的白色格子建边,求最大匹配,答案为最大匹配/2,因为是双向图.最后输出匹配边时,当找到一组匹配边记得将该边标记,以防重复计算. #include <stdio.h> #include <algorithm> #include <set> #inc

HDU——T 1507 Uncle Tom&#39;s Inherited Land*

http://acm.hdu.edu.cn/showproblem.php?pid=1507 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3671    Accepted Submission(s): 1554Special Judge Problem Description Your old uncle Tom inherited

hdu 1507 Uncle Tom&#39;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): 1853    Accepted Submission(s): 769 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

HDU_1507_Uncle Tom&#39;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): 2208    Accepted Submission(s): 913 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

ZOJ 1516 Uncle Tom&#39;s Inherited Land

题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻的两块地组成,那么我们就将相邻的两块地一块放在X集合,一块放在Y集合 所有放在X集合中的土地互不影响(也就是任意两个在X中的土地不形成长方形) 那么我们可以看作土地上 0101010 1010101 0101010 1010101 比如这样标注的,那么0所对应的空地就放入集合X,并不断添加一个X的标

Uncle Tom&#39;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): 203 Accepted Submission(s): 132   Problem Description Your old uncle Tom inherited a piece of land from his great-great-