車的放置

AcWing-373-車的放置open in new window

分析

见《进阶指南》第427页。

实现

#include <iostream>
#include <cstring>
using namespace std;

const int N = 210;
int n, m, k;
int a[N][N]; // 邻接矩阵
int ln, rn;
bool mark[N];
int match[N];

bool dfs (int cur) {
    for (int i = 1; i <= rn; ++ i) {
        int to = i;
        if (a[cur][to] == -1) continue;
        if (mark[to] == true) continue;
        mark[to] = true;
        if (match[to] == 0 || dfs(match[to]) == true) {
            match[to] = cur;
            return true;
        }
    }
    return false;
}

int main () {
    cin >> n >> m >> k;
    for (int i = 1, x, y; i <= k; ++ i) {
        cin >> x >> y;
        a[x][y] = -1;
    }

    ln = n, rn = m;
    int res = 0;
    for (int i = 1; i <= ln; ++ i) {
        memset(mark, 0, sizeof(mark));
        if (dfs(i) == true) ++ res;
    }
    cout << res << endl;
    return 0;
}
最后修改于: