1. 程式人生 > 實用技巧 >HDU 1179:Ollivanders: Makers of Fine Wands since 382 BC.

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;
    }
}