Question: Given the relationship between some students and courses, ask if each course can correspond to only one student.
Solution: each edge corresponds to a selection relationship. The problem is to find whether a complete match exists.
CodeAs follows:
# Include <iostream> # Include <Cstdlib> # Include <Cstring> # Include <Cstdio> # Include <Algorithm> Using Namespace STD; Int P, N; Char G [ 105 ] [305 ], Vis [ 305 ]; Int Match [ 305 ]; Bool PATH ( Int U ){ For ( Int I = 1 ; I <= N; ++ I ){ If (! G [u] [I] | Vis [I]) Continue ; Vis [I] = 1 ; If (! Match [I] | PATH (Match [I]) {match [I] = U; Return True ;}} Return False ;} Bool Query (){ Int Ret =0 ; Memset (match, 0 , Sizeof (MATCH )); For ( Int I = 1 ; I <= P; ++ I) {memset (VIS, 0 , Sizeof (VIS); RET + = PATH (I );} Return Ret =P ;} Int Main (){ Int T, num, C; scanf ( " % D " ,& T ); While (T -- ) {Memset (G, 0 , Sizeof (G); scanf ( " % D " , & P ,& N ); For ( Int I = 1 ; I <= P; ++ I) {scanf ( " % D " ,& Num ); For ( Int J = 1 ; J <= num; ++ J) {scanf ( " % D " ,& C); G [I] [C] = 1 ;} Printf (query () ? " Yes \ n " : " No \ n " );} Return 0 ;}