匈牙利 - 模板

int nx, ny;
int edge[maxn][maxn];
int cx[maxn], cy[maxn];
bool vis[maxn];

int path(int u) {
    int v;
    for (v = 1; v <= ny; v++) {
        if (edge[u][v] && !vis[v]) {
            vis[v] = true;
            if (cy[v] == -1 || path(cy[v])) {
                cx[u] = v;
                cy[v] = u;
                return 1;
            }
        }
    }
    return 0;
}

int maxmatch() {
    int res = 0;
    memset(cx, -1, sizeof(cx));
    memset(cy, -1, sizeof(cy));
    for (int i = 1; i <= nx; i++) {
        if (cx[i] == -1) {
            memset(vis, 0, sizeof(vis));
            res += path(i);
        }
    }
    return res;
}

你可能感兴趣的:(匈牙利 - 模板)