poj 1469 COURSES 最大二分匹配
这题也是赤裸裸的最大二分匹配。在最后加个判断即可。
注意,用cin会超时,要用scanf
#include<iostream> using namespace std; const int MAX = 305; 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; int cases; cin >> cases; while (cases--) { scanf("%d%d", &n, &m); memset(arcs, false, sizeof(arcs)); memset(match, 0, sizeof(match)); for (int i = 1; i <= n; i++) { scanf("%d", &k); for (int j = 0; j < k; j++) { scanf("%d", &num); arcs[i][num] = true; } } int ans = 0; for (int i = 1; i <= n; i++) { memset(isvisit, false, sizeof(isvisit)); if (find(i)) ans++; } if (ans == n) printf("YES\n"); else printf("NO\n"); } return 0; }