Poj (1, 1469) Courses

Source: Internet
Author: User
Students have their own favorite courses. Therefore, for a course, multiple students may like it, but we want to have candidates for none of them. The first data indicates the number of tests, followed by P courses and N students,

In the next row of P, the first letter represents course 1, 2... P. the first letter of each line of I is the student who loves this subject. Enter the student ID.

Output: if the request is met, "yes" is output; otherwise, "no" is output ".

Analysis:

This is a allocation problem. Naturally, we think of the maximum matching of a bipartite graph. A course is a collection, and a student is a collection. There are multiple associations between students and courses, but between students and students, and between courses. This is easy to do. Use the Hungarian arithmetic to obtain the maximum number of matches. As long as the number of matches is greater than or equal to the total number of subjects.

 

# Include "stdio. H"
# Include "string. H"
Int map [1000] [1000];
Int link [1000], Mark [1000];
Int n, m;
Int find (int K)
{
Int I;
For (I = 1; I <= m; I ++)
{
If (MARK [I] = 0 & map [k] [I])
{
Mark [I] = 1;
If (link [I] =-1 | find (link [I])
{
Link [I] = K;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
Int I, K, H, P;
Scanf ("% d", & P );
While (p --)
{
Scanf ("% d", & N, & M );
Memset (MAP, 0, sizeof (MAP ));
Memset (link,-1, sizeof (Link ));
For (I = 1; I <= N; I ++)
{
Scanf ("% d", & K );
While (k --)
{
Scanf ("% d", & H );
Map [I] [H] = 1;
}
}
Int sum = 0;
For (I = 1; I <= N; I ++)
{
Memset (mark, 0, sizeof (Mark ));
Sum + = find (I );
}
If (sum = N)
Printf ("Yes \ n ");
Else
Printf ("NO \ n ");
}
Return 0;
}

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.