题目链接
解题思路:
- 以学生为一边,课程为一边,将学生可以选的课程直接相连即可
- 之后,二分图匹配
AC代码:
// C - Courses HDU - 1083.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxm = 40000;
int T, P, N, tot = 1;
int match[500], vis[500];
int graph[500][500];
bool dfs(int x) {
for (int i = 1; i <= P; i++) {
if (graph[x][i] && !vis[i]) {
vis[i] = 1;
if (match[i] == -1 || dfs(match[i])) {
match[i] = x;
return true;
}
}
}
return false;
}
int Maxmatch() {
int sum = 0;
memset(match, -1, sizeof(match));
for (int i = 1; i <= N; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) sum += 1;
}
return sum;
}
int main() {
cin >> T;
while (T--) {
memset(graph, 0, sizeof(graph));
tot = 1;
cin >> P >> N;
int num, x;
for (int i = 1; i <= P; i++) {
cin >> num;
for (int j = 1; j <= num; j++) {
cin >> x;
graph[x][i] = 1;
}
}
if (P > N) {//提前先判断P与N之间关系是否能达到题目要求
cout << "NO" << endl;
continue;
}
int ans = Maxmatch();
if (ans < P) cout << "NO" << endl;//判断是否达到条件
else cout << "YES" << endl;
}
return 0;
}