zoukankan      html  css  js  c++  java
  • HDU 1507 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): 1247    Accepted Submission(s): 542
    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
     
    Recommend
    LL
     

     要输出任意一组解。

    一开始时两边都是n*m-k个点做的,答案输出一半,但是错掉了,匹配数没有问题,就是输出解会出错。

    后来按照奇偶分成两部分就可以了

    #include <stdio.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <vector>
    using namespace std;
    const int MAXN = 510;
    int uN,vN;//u,v的数目,使用前面必须赋值
    int g[MAXN][MAXN];//邻接矩阵
    int linker[MAXN];
    bool used[MAXN];
    bool dfs(int u)
    {
        for(int v = 0; v < vN;v++)
            if(g[u][v] && !used[v])
            {
                used[v] = true;
                if(linker[v] == -1 || dfs(linker[v]))
                {
                    linker[v] = u;
                    return true;
                }
            }
        return false;
    }
    int hungary()
    {
        int res = 0;
        memset(linker,-1,sizeof(linker));
        for(int u = 0;u < uN;u++)
        {
            memset(used,false,sizeof(used));
            if(dfs(u))res++;
        }
        return res;
    }
    int a[110][110];
    int b[100];
    int main()
    {
        int n,m,k;
        int u,v;
        while(scanf("%d%d",&n,&m)==2)
        {
            if(n == 0 && m == 0)break;
            scanf("%d",&k);
    
            memset(a,0,sizeof(a));
            while(k--)
            {
                scanf("%d%d",&u,&v);
                u--;v--;
                a[u][v] = -1;
            }
            int index = 0;
            for(int i = 0;i < n;i++)
                for(int j = 0;j < m;j++)
                     if(a[i][j]!=-1)
                     {
                         b[index] = i*m + j;
                         a[i][j] = index++;
                     }
            uN = vN = index;
            memset(g,0,sizeof(g));
            for(int i = 0;i < n;i++)
                for(int j= 0;j < m;j++)
                    if(a[i][j]!=-1 && (i+j)%2==1)
                    {
                        u = a[i][j];
                        if(i > 0 && a[i-1][j]!=-1)
                            g[u][a[i-1][j]]=1;
                        if(i < n-1 && a[i+1][j]!=-1)
                            g[u][a[i+1][j]]=1;
                        if(j > 0 && a[i][j-1]!=-1)
                            g[u][a[i][j-1]]=1;
                        if(j < m-1 && a[i][j+1]!=-1)
                            g[u][a[i][j+1]]=1;
                    }
            int ans = hungary();
            printf("%d
    ",ans);
            for(int i = 0;i <vN;i++)
                if(linker[i]!=-1)
                {
                    int x1 = b[i]/m;
                    int y1 = b[i]%m;
                    int x2 = b[linker[i]]/m;
                    int y2 = b[linker[i]]%m;
                    printf("(%d,%d)--(%d,%d)
    ",x1+1,y1+1,x2+1,y2+1);
                }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    堆排序
    append、appendTo、prepend、prependTo、before、insertBefore、after、insertAfter、replaceAll方法被调用后,原本在页面上显示的元素会消失
    jQuery中的 $.ajax的一些方法
    attr VS prop 区别
    Canvas---clearRect()清除圆形区域
    HTML5 FormData方法介绍
    MongoDB学习笔记——数据库的创建与初始
    es6学习---.babelrc文件
    【转载】基于webpack构建react项目
    node常用模块---path
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3219031.html
Copyright © 2011-2022 走看看