参考题意:https://www.cnblogs.com/qq2424260747/p/4713748.html
注意:要用scanf输入输出!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=305; 6 int line[N][N],used[N],student[N]; 7 int n,m;//n课程数,m学生数 8 bool find(int x)//模板 9 { 10 int j; 11 for (j=1;j<=m;j++) 12 { 13 if (line[x][j]==true && used[j]==false) 14 { 15 used[j]=1; 16 if (student[j]==0 || find(student[j])) 17 { 18 student[j]=x; 19 return true; 20 } 21 } 22 } 23 return false; 24 } 25 int main() 26 { 27 // freopen("in.txt","r",stdin); 28 int t; 29 scanf("%d",&t); 30 while (t--) 31 { 32 memset(line,0,sizeof(line)); 33 memset(student,0,sizeof(student)); 34 scanf("%d%d",&n,&m); 35 for (int i=1;i<=n;i++) 36 { 37 int temp; 38 scanf("%d",&temp); 39 while (temp--) 40 { 41 int tem; 42 scanf("%d",&tem); 43 line[i][tem]=1; 44 } 45 } 46 int ans=0; 47 for (int i=1;i<=n;i++) 48 { 49 memset(used,0,sizeof(used)); 50 if (find(i)) 51 { 52 ans++; 53 } 54 } 55 if (ans==n) 56 { 57 printf("YES "); 58 } 59 else 60 { 61 printf("NO "); 62 } 63 } 64 65 return 0; 66 }