[算法] poj 1274 The Perfect Stall (匈牙利)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 205;
bool map[N][N];
int mat[N];
bool vis[N];
int n, m;
bool dfs(int u) {
    for(int i = 1; i <= m; i++) {
        if(map[u][i] && !vis[i]) {
            vis[i] = true;
            if(mat[i] == 0 || dfs(mat[i])) {
                mat[i] = u;
                return true;
            }
        }
    }
    return false;
}
int maxMatch() {
    memset(mat, 0, sizeof(mat));
    int count = 0;
    for(int i = 1; i <= n; i++) {
        memset(vis, 0, sizeof(vis));
        if(dfs(i)) count++;
    }
    return count;
}
int main() {
    while(scanf("%d%d", &n, &m) == 2) {
        memset(map, 0, sizeof(map));
        int ca, j;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &ca);
            while(ca--) {
                scanf("%d", &j);
                map[i][j] = 1;
            }
        }
        printf("%d\n", maxMatch());
    }
    return 0;
}
posted @ 2013-04-15 20:07  小尼人00  阅读(120)  评论(0编辑  收藏  举报