View more problem solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted with the source: http://blog.csdn.net/wangjian8006
There are many courses in the university. Now James needs to select a course. He is a learner, so he wants to select as many courses as possible,
There are n courses in the school, and according to the school, there are 12 classes per week every day, so there are 7*12 classes per week.
Enter the first action N, which indicates that there are n courses.
In the next n rows, the first number X in each row indicates that this course needs to be used X times in the week.
Then follow the X pairs of numbers. The first D represents the day of the week, and the second c Represents the class of the day.
If several courses are in Class C that day, you need to select one of them, instead of multiple courses.
Now it is required to calculate the maximum number of courses that James can choose
Problem-solving ideas: we will regard this question as a bipartite graph, the left represents the course, the right represents a class, code (D-1) * 12 + C, this means that every lesson of the week is considered as a node.
In this way, if the course corresponds to that class, there will be even one edge. Then, find the maximum matching value for the bipartite graph.
/* Memory 232 ktime 32 Ms */# include <iostream> using namespace STD; # define maxv 310int n, m; bool map [maxv] [maxv], use [maxv]; int link [maxv]; bool DFS (int x) {int I, j; for (I = 1; I <= m; I ++) {If (Map [x] [I] &! Use [I]) {use [I] = 1; j = link [I]; Link [I] = x; If (DFS (j) | j =-1) return 1; Link [I] = J;} return 0;} int Hungary () {int I, num = 0; memset (link, -1, sizeof (Link); for (I = 1; I <= N; I ++) {memset (use, 0, sizeof (use )); if (DFS (I) num ++;} return num;} int main () {int I, num, a, B; while (~ Scanf ("% d", & N) {memset (MAP, 0, sizeof (MAP); M = 7*12; for (I = 1; I <= N; I ++) {scanf ("% d", & num); While (Num --) {scanf ("% d", &, & B); map [I] [(A-1) * 12 + B] = 1 ;}} printf ("% d \ n", Hungary ());} return 0 ;}