Question http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1083
This is a simple question of Maximizing matching.
There are two methods
AC1
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; bool map [101] [301]; bool mark [301]; int CX [101], CY [301]; // CX indicates how many courses Cy indicates students Oh int P, N; int findpath (int u) // find the augmented path Oh {int I; for (I = 1; I <= N; I ++) {If (Map [u] [I] &! Mark [I]) // pay attention to the U location {mark [I] = 1; if (CY [I] =-1 | findpath (CY [I]) {CY [I] = u; CX [u] = I; return 1 ;}}return 0 ;}void maxmatch () // find the maximum match. {int I, j; int res = 0; for (I = 1; I <= P; I ++) CX [I] =-1; for (j = 1; j <= N; j ++) CY [J] =-1; for (I = 1; I <= P; I ++) {If (CX [I] =-1) {for (j = 1; j <= N; j ++) MARK [J] = 0; res + = findpath (I) ;}} if (RES >=p) {cout <"yes" <Endl; return ;} else cout <"no" <Endl;} int main () {int I, J, K, M; CIN> K; while (k --) {memset (MAP, false, sizeof (MAP); CIN> P> N; for (I = 1; I <= P; I ++) {CIN> J; For (int h = 1; H <= J; H ++) {CIN> m; Map [I] [m] = 1 ;}} maxmatch () ;}return 0 ;}
Ac2
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; bool map [301] [301]; bool mark [301]; int link [301]; // CX indicates the number of courses. Cy indicates the number of students. Int P, N; int findpath (int u) {int I; for (I = 1; I <= N; I ++) {If (Map [u] [I] &! Mark [I]) {mark [I] = 1; // is it CX or Cy? If (! Link [I] | findpath (link [I]) {link [I] = u; return 1 ;}} return 0 ;}void maxmatch () {int I, j; int res = 0; For (j = 1; j <= N; j ++) link [J] = 0; for (I = 1; I <= P; I ++) {for (j = 1; j <= N; j ++) MARK [J] = 0; Res + = findpath (I );} if (RES> = P) {cout <"yes" <Endl; return;} else cout <"no" <Endl;} int main () {int I, J, K, M; CIN> K; while (k --) {memset (MAP, false, sizeof (MAP); memset (link, false, sizeof (Link); CIN> P> N; for (I = 1; I <= P; I ++) {CIN> J; for (int h = 1; H <= J; H ++) {CIN> m; Map [I] [m] = 1 ;}} maxmatch (); // cout <"ddddddddddd1" <Endl;} return 0 ;}