您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页hdu 4619 Warm up 2【二分匹配】

hdu 4619 Warm up 2【二分匹配】

来源:宝玛科技网

Warm up 2

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2191    Accepted Submission(s): 1027

Problem Description

Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.

Input

There are multiple input cases.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.

Output

For each test case, output the maximum number of remaining dominoes in a line.

Sample Input

2 3

0 0

0 3

0 1

1 1

1 3

4 5

0 1

0 2

3 1

2 2

0 0

1 0

2 0

4 1

3 2

0 0

Sample Output

4

6

Author

SYSU

Source


题目大意:

给你N个占(x,y)和(x+1,y)两个格子的骨牌。以及M个占(x,y)和(x,y+1)两个格子的骨牌。我们想要将其保证没有重叠的骨牌,问最多能够剩下几个骨牌。


思路:


1、首先注意到坐标范围只在100以内,并且保证N,M的总数最大只有2000,那么我们开一个数组map【i】【j】,表示坐标(i,j)的编号,我们一边输入,一边将点进行编号。


2、因为每个骨牌都占据两个位子,那么其能够构成二分图,其间可以用一个无向边相连。那么如果对应一个点有可能有覆盖,那么其二分图的最大匹配问题,能够使得这个点最多只匹配一个骨牌。

那么我们将输入进来的每个骨牌的两个点之间连一条无向边,然后跑一遍匈牙利最大二分匹配算法,求得的最大二分匹配数,就是最终的答案。

因为我们建立的是无向边,那么结果是最大二分匹配数/2.


3、注意初始化


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int map[200][200];
int vis[5000];
int match[5000];
vector<int >mp[5000];
int find(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u;return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        int cont=1;
        memset(match,-1,sizeof(match));
        memset(map,0,sizeof(map));
        for(int i=1;i<=4500;i++)mp[i].clear();
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(map[x][y]==0)
            {
                map[x][y]=cont++;
            }
            if(map[x+1][y]==0)
            {
                map[x+1][y]=cont++;
            }
            int u=map[x][y],v=map[x+1][y];
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(map[x][y]==0)
            {
                map[x][y]=cont++;
            }
            if(map[x][y+1]==0)
            {
                map[x][y+1]=cont++;
            }
            int u=map[x][y],v=map[x][y+1];
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        int output=0;
        for(int i=1;i<cont;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))output++;
        }
        printf("%d\n",output/2);
    }
}





因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务