zoukankan      html  css  js  c++  java
  • zoj 1516(二分图最大匹配)

    简单的二分图。

    思路:对于每个不是ponds的点标号,建图时把每个点拆成两个,然后在这两部分进行最大匹配,具体的建边的方法是, 每个标号点,都找和他相邻的点连边。 

    最后用匈牙利算法算出最大匹配后, 将总数除以2就是结果

    Uncle Tom's Inherited Land

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 produce one line of output, containing an integer value representing the maximum number of properties which can be sold.

    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 3


    Source: South America 2002, Practice

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    int n,m,id;
    int g[110][110];
    int k[55][55];
    int up[4] = {1,-1,0,0};
    int rl[4] = {0,0,1,-1};
    int pre[55],mark[55];
    
    int dfs(int s)
    {
        for(int i=1;i<id;i++)
        {
            if(mark[i] == 1||k[s][i]==0) continue;
            mark[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=s;
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        while( scanf("%d%d",&n,&m) && (n+m) )
        {
            memset(k,0,sizeof(k));
            memset(g,0,sizeof(g));
            int k1;
            scanf("%d",&k1);
            for(int i=0;i<k1;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                g[x][y]=-1;
            }
            id=1;
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                {
                    if(g[i][j]!=-1) g[i][j]=id++;
                }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(g[i][j]==-1) continue;
                    int ti,tj;
                    for(int k2=0;k2<4;k2++)
                    {
                        ti=i+up[k2];
                        tj=j+rl[k2];
                        if( (ti>=1&&ti<=n)&&(tj>=1&&tj<=m) && g[ti][tj]!=-1)
                        {
                            k[ g[i][j] ][ g[ti][tj] ]=1;
                        }
                    }
                }
            }
            int sum=0;
            memset(pre,-1,sizeof(pre));
            for(int i=1;i<id;i++)
            {
                memset(mark,0,sizeof(mark));
                sum += dfs(i);
            }
            printf("%d\n",sum/2);
        }
        return 0;
    }
  • 相关阅读:
    计算位数
    素数的判断(大数据,大规模)
    Patting Heads
    Jury Jeopardy (这是一道单纯的模拟题)
    POJ 2229 Sumsets(规律)
    OJ 26217 :Work Scheduling(贪心+优先队列)
    牛客Professional Manager(并查集)
    DJ 算法的队列优先优化
    优先队列priority_queue的简单应用
    node.js服务端存储用户密码md5加密
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3023520.html
Copyright © 2011-2022 走看看