hdu 4619 Warm up 2 (二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619

题意:

平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不会相互覆盖。水平放置的牌和竖直放置的牌可能相互覆盖,现在要移去一些牌,使得剩下的牌任何两张都不会相互覆盖,问桌面上最多能剩多少张牌。

分析:

如果把每张牌看作一个结点,则共有两类结点,容易联想到二分图。另外,同方向的牌不会相互覆盖,不同方向的可能相互覆盖,易想到二分图的一个重要性质:同类结点间不会连边,不同结点间可以连边。所以显然是这方面的问题了。

解题思路:

根据上述分析,这是一个二分图问题,每张牌作为一个结点,水平放置的成点集X,竖直放置的成点集Y,若两张牌相互覆盖,则两点间连边。这样建图后,一个“覆盖点”对应一条边,成了裸的“最小点覆盖”问题,即:结点数-最大二分匹配数。

代码:

#include <cstdio>
#include <cstring>
using namespace std;
#define N 1200

bool mp[N][N];
int x[N], y[N], f[N], rm[N], lm[N];
int n, m;

bool path(int s)
{
    for(int i = 1; i <= m; i++)
        if(f[i] == 0 && mp[s][i])
        {
            f[i] = 1;
            if(rm[i] == 0 || path(rm[i]))
            {
                rm[i] = s; lm[s] = i;
                return true;
            }
        }
    return false;
}

int MaxMatch()
{
    memset(rm, 0, sizeof(rm));
    memset(lm, 0, sizeof(lm));
    int ans = 0;
    for(int i = 1; i <= n; i++)
    if(!lm[i])
    {
        memset(f, 0, sizeof(f));
        if(path(i)) ans++;
    }
    return ans;
}
int main()
{
    int i, j;
    while(~scanf("%d %d", &n, &m), n||m)
    {
        int a, b;
        for(i = 1; i <= n; i++)
            scanf("%d %d", &x[i], &y[i]);
        memset(mp, false, sizeof(mp));
        for(i = 1; i <= m; i++)
        {
            scanf("%d %d", &a, &b);
            for(j = 1; j <= n; j++)
            if( (a == x[j] && b == y[j]) ||
                (a == x[j] && b == y[j] - 1) ||
                (a == x[j] + 1 && b == y[j]) ||
                (a == x[j] + 1 && b == y[j]-1)
            ) mp[j][i] = true;
        }
        int ans = MaxMatch();
        printf("%d\n", n+m-ans);
    }
    return 0;
} 

时间: 2024-11-08 10:25:38

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): 1754    Accepted Submission(s): 781 Problem Description Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizont

hdu 2768 Cat vs. Dog (二分匹配)

Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1422    Accepted Submission(s): 534 Problem Description The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunch

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): 1895    Accepted Submission(s): 862 Problem Description Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizont

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

HDU - 1045 Fire Net(二分匹配)

Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to s

HDU 2063 过山车(二分匹配入门)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.net/dark_scope/article/details/8880547 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using nam

HDU 2063 过山车 (二分匹配之匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12475    Accepted Submission(s): 5446 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做pa

hdu 1150 Machine Schedule (经典二分匹配)

//A组n人 B组m人 //最多有多少人匹配 每人仅仅有匹配一次 # include<stdio.h> # include<string.h> # include<algorithm> using namespace std; int n,m,k; int pp[1100][1100],map[1100],vis[1100]; int bfs(int x)//二分匹配模板 { for(int i=1;i<=m;i++)//B组中的人来迎合匹配 { if(!vis[

hdu 2063 过山车 二分匹配(匈牙利算法)

简单题hdu2063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13500    Accepted Submission(s): 5913 Problem Description RPG girls今天和大家一起去游乐场玩