Acm hdu 1083 courses (Binary match, maximum match)

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.