Loading

【模板】二分图最大匹配

n,m分别为两边的点数,E为边数。

int e[maxn][maxn];
int n, m;
int    vis[maxn], match[maxn];

bool dfs(int x) {
    for (int i = 1; i <= m; i++) {
        if (e[x][i]) {
            if (vis[i]) continue;
            vis[i] = 1;
            if (!match[i] || dfs(match[i])) {
                match[i] = x;
                return true;
            }
        }
    }
    return false;
}

int Match() {
    int cnt = 0;
    memset(match, 0, sizeof match);
    for (int i = 1; i <= n; i++) {
        memset(vis, 0, sizeof vis);
        if (dfs(i)) cnt++;
    }
    return cnt;
}

int main() {
    int E;
    scanf("%d%d%d", &n, &m, &E);
    for (int i = 1; i <= E; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        e[x][y] = 1;
    }
    printf("%d", Match());
}

 

posted @ 2020-07-17 14:00  MQFLLY  阅读(169)  评论(0编辑  收藏  举报