zoukankan      html  css  js  c++  java
  • HDU1507二分图

    Uncle Tom's Inherited Land*

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3445    Accepted Submission(s): 1452
    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
     题意:n*m的网格,有的不能用,要求相邻的两个小方格为一组,问最多有多少组。
    代码:
    //简单的二分匹配,相邻的且可用的网格之间建边,求玩=完二分图之后link数组存储的就是答案。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    int vis[10004],link[10004],nu1[10004],nu2[10004],pond[10004];
    int Mu,Mv,n,m,k,last;
    vector<int>g[10004];
    bool dfs(int x)
    {
        for(int i=0;i<g[x].size();i++){
            int y=g[x][i];
            if(!vis[y]){
                vis[y]=1;
                if(link[y]==-1||dfs(link[y])){
                    link[y]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    int Maxcon()
    {
        int ans=0;
        memset(link,-1,sizeof(link));
        for(int i=1;i<=n*m;i++){
            if(pond[i]) continue;
            memset(vis,0,sizeof(vis));
            if(dfs(i)) ans++;
        }
        return ans;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)&&(n+m)){
            int x,y;
            memset(pond,0,sizeof(pond));
            scanf("%d",&k);
            while(k--){
                scanf("%d%d",&x,&y);
                pond[(x-1)*m+y]=1;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    int id=(i-1)*m+j;
                    g[id].clear();
                    if(i>1&&pond[id-m]==0) g[id].push_back(id-m);
                    if(j>1&&pond[id-1]==0) g[id].push_back(id-1);
                    if(i<n&&pond[id+m]==0) g[id].push_back(id+m);
                    if(j<m&&pond[id+1]==0) g[id].push_back(id+1);
                }
            }
            int ans=Maxcon()/2;
            printf("%d
    ",ans);
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    int id=(i-1)*m+j;
                    if(link[id]==-1||link[link[id]]==-1) continue;
                    printf("(%d,%d)--(%d,%d)
    ",i,j,(link[id]-1)/m+1,link[id]%m==0?m:link[id]%m);
                    link[id]=link[link[id]]=-1;
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    MySQL 5.1.73升级为MySQL 5.5.35详解
    MySQL 常用show命令
    MySQL 用户与授权管理详解
    MySQL 日志管理详解
    MySQL 5.5.35 单机多实例配置详解
    mysql启动与关闭(手动与自动)
    hduTHE MATRIX PROBLEM(差分约束)
    在 iPad和 iPhone的浏览器上查看网页源代码
    《30天自制操作系统》之——第3天
    python network programming tutorial
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6504690.html
Copyright © 2011-2022 走看看