hdu1507二分图最大匹配奇偶建边

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)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 2500

using namespace std;
int map[maxn][maxn];
int num1,num2;
int n,m,k;
vector<int>g[maxn];
bool vis[maxn];
int lef[maxn];

void init()
{
    for(int i=0;i<=maxn;i++)
        g[i].clear();
    for(int i=0;i<=maxn;i++)
        lef[i]=-1;

}
bool match(int u)
{
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            vis[v]=true;
            if(lef[v]==-1||match(lef[v]))
            {
                lef[v]=u;
                return true;
            }
        }
    }
        return false;

}
int solve()
{
    int ans=0;
    for(int i=1;i<=num1;i++)
    {
        memset(vis,0,sizeof(vis));
        if(match(i))
            ans++;
    }
    return ans;
}
struct node
{
    int x,y;
    node(){}
    node(int x,int y):x(x),y(y){}
    int link(node&rhs)
    {
        if(x==rhs.x&&y==rhs.y+1)
            return 1;
        if(x==rhs.x&&y==rhs.y-1)
            return 1;
        if(x==rhs.x-1&&y==rhs.y)
            return 1;
        if(x==rhs.x+1&&y==rhs.y)
            return 1;
            return 0;
    }

}node1[maxn],node2[maxn];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {if(n==0&&m==0)
    break;
    scanf("%d",&k);
        init();
        memset(map,0,sizeof(map));
        num1=0;
        num2=0;
        while(k--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            map[u][v]=1;
        }
        for(int i=1;i<=n;i++)
         for(int j=1;j<=m;j++)
            if(map[i][j]==0)
            {
                if((i+j)%2==0)
                    node1[++num1]=node(i,j);
                else
                    node2[++num2]=node(i,j);
            }
            for(int i=1;i<=num1;i++)
                for(int j=1;j<=num2;j++)
                if(node1[i].link(node2[j]))
                g[i].push_back(j);
            printf("%d\n",solve());
            for(int v=1;v<=num2;v++)
            {
                int u=lef[v];
                if(u!=-1)
                    printf("(%d,%d)--(%d,%d)\n",node1[u].x,node1[u].y,node2[v].x,node2[v].y);

            }
            printf("\n");

    }
    return 0;
    }

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/88540073