Question:
There are P courses and N students, and each student is free to choose courses (0 to P). Now we need to establish a committee, can I find a set of student representatives for each course? One student can only represent one course.
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1083
Simple binary matching, using HungaryAlgorithmYou can solve the problem by using a fixed method as the exercise template!
Program:
# Include < Stdio. h >
# Include < Iostream >
Using Namespace STD;
# Define Maxn 305
Int G [maxn] [maxn];
Int UN, vn;
Int Linker [maxn];
Bool Used [maxn];
Bool DFS ( Int U)
{
Int V;
For (V = 1 ; V <= Vn; V ++ )
If (G [u] [v] &&! Used [v])
{
Used [v] = True ;
If (Linker [v] =- 1 | DFS (linker [v])
{
Linker [v] = U;
Return True ;
}
}
Return False ;
}
Int Hungary ()
{
Int Res = 0 , U;
Memset (linker, - 1 , Sizeof (Linker ));
For (U = 1 ; U <= UN; u ++ )
{
Memset (used, 0 , Sizeof (Used ));
If (DFS (u) res ++ ;
}
Return Res;
}
Int Main ()
{
Int U, V;
Int T, I, N;
Scanf ( " % D " , & T );
While (T -- )
{
Scanf ( " % D " , & UN, & VN );
Memset (G, 0 , Sizeof (G ));
For (U = 1 ; U <= UN; u ++ )
{
Scanf ( " % D " , & N );
While (N -- )
{
Scanf ( " % D " , & V );
G [u] [v] = 1 ;
}
}
If (UN = Hungary () printf ( " Yes \ n " );
Else Printf ( " No \ n " );
}
Return 0 ;
}
Sorry, it's too simple to add comments !!! For more information about the Hungarian algorithm, see the previous times about binary matching.Article!