Algorithm concept: bipartite graph.
Hungarian algorithm, simple template question. Maximum number of matches = P (number of courses), and "yes" is printed ". The input must use scanf. Cin times out.
// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # include <queue> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t;} template <CL Ass T> string tostring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // template end (General Part) # define ifs CIN # define Max 405int juzhen [Max] [Max]; int used [Max]; int mat [Max]; void Init () {memset (juzhen, 0, sizeof (juzhen);} int augment (int s, int N, int X) {int I; for (I = s; I <= N; I ++) {If (! Used [I] & juzhen [x] [I]) {used [I] = 1; if (MAT [I] =-1 | augment (S, N, mat [I]) {mat [I] = x; return 1 ;}}return 0 ;}int Hungary (int s, int N) {int I, sum = 0; memset (MAT,-1, sizeof (MAT); for (I = s; I <= N; I ++) {memset (used, 0, sizeof (used); If (augment (S, N, I) {sum ++ ;}} return sum ;} // [Graph Theory 02] bipartite graph 1003 coursesint main () {// ifstream ifs ("shuju.txt", IOS: In); INT cases, P, N; int, b; ifs> cases; while (cases --) {// ifs> P> N; scanf ("% d", & P, & N ); memset (juzhen, 0, sizeof (juzhen); For (INT I = 1; I <= P; I ++) {// ifs>; scanf ("% d", & A); For (Int J = 0; j <A; j ++) {// ifs> B; scanf ("% d", & B); juzhen [I] [p + B] = 1; // juzhen [M + B] [a] = 1 ;}} int max_match = Hungary (1, P + n); If (max_match = P) {printf ("Yes \ n ");} else {printf ("NO \ n") ;}} return 0 ;}