Problem: poj2239
Analysis:
This question shows the course duration for each course. Finding the maximum number of courses can be converted to the maximum matching problem in the bipartite graph.
Set set a as the course set, Set B as the class time set, and create a bipartite graph based on the input. The largest course selection book is the maximum matching number of the Bipartite Graph, which can be solved using the Hungary algorithm.
AC code
1 //Memory: 252K Time: 16MS 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 const int maxl = 301; 9 const int maxc = 85;10 int edge[maxc][maxl];11 int ne[maxc];12 int match[maxl];13 bool vis[maxl];14 int n, t;15 int a, b;16 17 bool findPath(int start)18 {19 for (int i = 0; i < ne[start]; i++){20 int current = edge[start][i];21 if (vis[current]) continue;22 vis[current] = 1;23 if ( !match[current] || findPath(match[current]) ){24 match[current] = start;25 return true;26 }27 }28 return false;29 }30 31 int solve()32 {33 memset(match, 0, sizeof(match));34 int cnt = 0;35 for (int i = 1; i <= 84; i++) {36 memset(vis, 0, sizeof(vis));37 if (findPath(i)) cnt++;38 }39 return cnt;40 }41 42 void input()43 {44 memset(edge, 0, sizeof(edge));45 memset(ne, 0, sizeof(ne));46 for (int i = 0; i < n; i++){47 scanf("%d", &t);48 while (t--){49 scanf("%d%d", &a, &b);50 int class_num = (a - 1) * 12 + b;51 edge[ class_num ][ ne[class_num]++ ] = i;52 }53 }54 }55 56 int main()57 {58 while ( ~scanf("%d", &n) ){59 input();60 int ans = solve();61 printf("%d\n", ans);62 }63 return 0;64 }