HDU 1179:Ollivanders: Makers of Fine Wands since 382 BC.

HDU - 1179

二分图介绍:匈牙利算法

模板二分图:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, m, f[maxn], ans, k, x;
bool mat[maxn][maxn];
int mac[maxn];
bool v[maxn];

int dfs(int x) {
    for (int i = 1; i <= n; ++i) 
        if (mat[x][i] && !v[i]) {
            v[i] = 1;
            if (!mac[i] || dfs(mac[i])) {
                mac[i] = x;
                return 1;
            }
        }
    return 0;
}

int main() {
    //freopen("in.txt", "r", stdin);
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    while (cin >> n && n) {
        memset(mac, 0, sizeof mac);
        memset(mat, 0, sizeof mat);
        cin >> m;
        for (int i = 1; i <= m; ++i) {
            cin >> k;
            while (k--)
                cin >> x, mat[i][x] = 1;
        }
        ans = 0;
        for (int i = 1; i <= m; ++i) {
            memset(v, false, sizeof v);
            if (dfs(i))ans++;
        }
            
        cout << ans << endl;
    }
}
posted @ 2020-09-16 20:03  Koshkaaa  阅读(160)  评论(0编辑  收藏  举报