poj The Perfect Stall 1274 最大二分匹配
赤裸裸的最大二分匹配入,直接套匈牙利算法模板
#include<iostream> using namespace std; const int MAX = 300; bool arcs[MAX][MAX]; bool isvisit[MAX]; int match[MAX]; int n, m; bool find(int u) { for (int i = 1; i <= m; i++) if (arcs[u][i] && !isvisit[i]) { isvisit[i] = true; if (!match[i] || find(match[i])) { match[i] = u; return true; } } return false; } int main() { int k; int num; while (cin >> n >> m) { memset(arcs, false, sizeof(arcs)); memset(match, 0, sizeof(match)); for (int i = 1; i <= n; i++) { cin >> k; for (int j = 0; j < k; j++) { cin >> num; arcs[i][num] = true; } } int ans = 0; for (int i = 1; i <= n; i++) { memset(isvisit, false, sizeof(isvisit)); if (find(i)) ans++; } cout << ans << endl; } return 0; }