hdu 1083 Courses (最大匹配)
Courses
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10769 Accepted Submission(s): 5077
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10769 Accepted Submission(s): 5077
Problem 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
Your program should read sets of data from a text file. The first line of the input file 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
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'll 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.
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 should not be any leading blanks at the start of the line.
An example of program input and output:
Sample Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Sample Output
YES
NO
YES
NO
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0xffffff 12 using namespace std; 13 const int my_max = 305; 14 15 int P, N, my_line[my_max][my_max], my_book[my_max], my_right[my_max], b, n, my_ans; 16 17 bool my_dfs(int x) 18 { 19 for (int i = 1; i <= N; ++ i) 20 { 21 if (!my_book[i] && my_line[x][i]) 22 { 23 my_book[i] = 1; 24 if (!my_right[i] || my_dfs(my_right[i])) 25 { 26 my_right[i] = x; 27 return true; 28 } 29 } 30 } 31 return false; 32 } 33 34 int main() 35 { 36 int t; 37 scanf("%d", &t); 38 while (t --) 39 { 40 my_ans = 0; 41 memset(my_line, 0, sizeof(my_line)); 42 memset(my_right, 0, sizeof(my_right)); 43 44 scanf("%d%d", &P, &N); 45 for (int i = 1; i <= P; ++ i) 46 { 47 scanf("%d", &n); 48 while (n --) 49 { 50 scanf("%d", &b); 51 my_line[i][b] = 1; 52 } 53 } 54 55 for (int i = 1; i <= P; ++ i) 56 { 57 memset(my_book, 0, sizeof(my_book)); 58 if (my_dfs(i)) my_ans ++; 59 } 60 61 if (my_ans == P) 62 printf("YES\n"); 63 else 64 printf("NO\n"); 65 } 66 return 0; 67 }