Courses
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:17153 |
|
Accepted:6740 |
Description
Consider a group of N students and P courses. each student visits zero, one or more than one courses. your task is to determine whether it is possible to form a committee of exactly p students that satisfies simultaneously the conditions:
- Every student in the committee represents a different course (a student can represent a course if he/she visits that course)
- Each course has a representative in the Committee
Input
Your program shocould read sets of data from the STD input. The first line of the input contains the number of the data sets. Each data set is presented in the following format:
P n
Count1 student1 1 student1 2... student1 count1
Count2 student2 1 student2 2... student2 count2
...
Countp studentp 1 studentp 2... studentp countp
The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) -The number of courses and N (1 <= n <= 300)-the number of students. the next P lines describe in sequence of the courses? From course 1 to course P, each line describing a course. the description of course I is a line that starts with an integer count I (0 <= count I <= N) representing the number of students visiting course I. next, after a blank, you must l find the count I students, visiting the course, each two consecutive separated by one blank. students are numbered with the positive integers from 1 to n.
There are no blank lines between consecutive sets of data. Input data are correct.
Output
The result of the program is on the standard output. for each input data set the program prints on a single line "yes" if it is possible to form a committee and "no" otherwise. there shoshould not be any leading blanks at the start of the line.
Sample Input
23 33 1 2 32 1 21 13 32 1 32 1 31 1
Sample output
Yesno
A total of N students and P courses are offered. One student can choose either of them.
For more or more courses, ask if:
1. Each student chooses a different course (that is, two students cannot choose the same course)
2. Each course has a representative (that is, all P courses have been successfully selected)
Note: students may not choose to attend classes.
The Hungarian algorithm for maximum matching:
# Include "stdio. H "# include" string. H "# define n 305int G [N] [N], link [N]; int mark [N], N, P; int find (int K) {int I; for (I = 1; I <= N; I ++) {If (G [k] [I] &! Mark [I]) {mark [I] = 1; if (! Link [I] | find (link [I]) {link [I] = K; return 1 ;}} return 0 ;}int main () {int I, t, M, V; scanf ("% d", & T); While (t --) {memset (G, 0, sizeof (g); memset (link, 0, sizeof (Link); scanf ("% d", & P, & N); for (I = 1; I <= P; I ++) {scanf ("% d", & M); While (M --) {scanf ("% d", & V ); G [I] [v] = 1 ;}} int ans = 0; for (I = 1; I <= P; I ++) {memset (mark, 0, sizeof (Mark); ans + = find (I);} If (ANS = P) printf ("Yes \ n "); else printf ("NO \ n");} return 0 ;}
Poj 1469 (hdu1083) Maximum match of courses