POJ3041 Asteroids(二分图最大匹配)

题目链接

分析:

暂略。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>

using namespace std;

const int maxn = 510;
int n;
bool G[maxn][maxn];
int linker[maxn];
bool used[maxn];

bool dfs(int u) {
    for(int v=0; v<n; v++)
    if(G[u][v] && !used[v]) {
        used[v] = true;
        if(linker[v] == -1 || dfs(linker[v])) {
            linker[v] = u;
            return true;
        }
    }
    return false;
}

int hungary() {
    int res = 0;

    memset(linker, -1, sizeof(linker));

    for(int u=0; u<n; u++) {
        memset(used, 0, sizeof(used));
        if(dfs(u)) res++;
    }

    return res;
}

int main() {
    int k, r, c;

   // freopen("my.txt", "r", stdin);

    while(scanf("%d%d", &n, &k) == 2) {
        memset(G, false, sizeof(G));

        for(int i=0; i<k; i++) {
            scanf("%d%d", &r, &c);
            G[--r][--c] = true;
        }

        int ans = hungary();

        printf("%d
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/tanhehe/p/3172418.html