AtCoder - 2D Plane 2N Points 二分图匹配 匈牙利算法模版

问题描述

Problem Statement
On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i-th red point are (ai,bi), and the coordinates of the i-th blue point are (ci,di).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.
问题OJ入口 < <

题解

还可参考hdu2063,这也是一道裸二分图最大匹配的问题,直接上匈牙利算法即可。
通过红色点dfs找蓝色点,套模版即可。

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e2 + 5;
bool used[MAX_N], match[MAX_N][MAX_N];
int girl[MAX_N];
bool fnd(int x, int n) {
    for (int j = 1; j <= n; j++) {
        if (match[x][j] && !used[j]) {
            used[j] = true;
            if (girl[j] == 0 || fnd(girl[j], n)) {
                girl[j] = x;
                return true;
            }
        }
    }
    return false;
}
int solve(int m, int n) {
    int ans = 0;
    for (int i = 1; i <= m; i++) {
        memset(used, false, sizeof(used));
        if (fnd(i, n)) ans++;
    }
    return ans;
}
int main() {
    int n, a, b, x[105], y[105];
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &a, &b);
        for (int j = 1; j <= n; j++) {
            if (x[j] < a && y[j] < b) match[j][i] = true;
        }
    }
    printf("%d\n", solve(n, n));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deaidai/article/details/79640643