Poj 1469 COURSES (max matching)

Source: Internet
Author: User

Question meaning:

There are N students and P courses, allowing you to determine whether it can constitute the maximum match.

Enter a T to indicate that there are T groups of test data;

In input N and P, P indicates there are P courses, and N indicates there are N students.

Followed by P rows, such:

A a1 a2 a3 a4 a5 --- the first line. 1 matches with a1, a2, a3, a4, and a5.

B b1 b2 b3 ----- Row 2. B matches with b1, b2, and b3.

YES if the number of matched students is equal to the number of students;

Otherwise, it is NO;

[Cpp]
# Include "stdio. h"
# Include "string. h"
# Define N 501
Int n, m, map [N] [N], mark [N], link [N];
Int dfs (int t)
{
Int I;
For (I = 1; I <= m; I ++)
{
If (! Mark [I] & map [t] [I])
{
Mark [I] = 1;
If (link [I] =-1 | dfs (link [I])
{
Link [I] = t;
Return 1;
}
}
}
Return 0;
}
Int MaxMatch ()
{
Int I, num;
Num = 0;
Memset (link,-1, sizeof (link ));
For (I = 1; I <= m; I ++)
{
Memset (mark, 0, sizeof (mark ));
If (dfs (I ))
Num ++;
}
Return num;
}
Int main ()
{
Int I, j, a, B, T, ans;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
Memset (map, 0, sizeof (map ));
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & );
For (j = 0; j <a; j ++)
{
Scanf ("% d", & B );
Map [I] [B] = 1;
}
}
Ans = MaxMatch ();
// Printf ("% d \ n", ans );
If (MaxMatch () = 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.