N节课,每节课在一个星期中的某一节,求最多能选几节课
好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱
建图: 每节课 与 时间有一条边
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 using namespace std; 8 const int N = 300 + 5; 9 int vis[N], Left[N]; 10 vector<int> g[N]; 11 int n; 12 bool mach(int u) 13 { 14 int Size = g[u].size(); 15 for (int i = 0; i < Size; i++) 16 { 17 int v = g[u][i]; 18 if (!vis[v]) 19 { 20 vis[v] = 1; 21 if (Left[v] == -1 || mach(Left[v])) 22 { 23 Left[v] = u; 24 return true; 25 } 26 } 27 } 28 return false; 29 } 30 int main() 31 { 32 while (scanf("%d", &n) != EOF) 33 { 34 int t, p, q; 35 for (int i = 0; i <= n; i++) 36 g[i].clear(); 37 for (int i = 1; i <= n; i++) 38 { 39 scanf("%d", &t); 40 for (int j = 1; j <= t; j++) 41 { 42 scanf("%d%d", &p, &q); 43 g[i].push_back(p * 12 + q); // 可以将每一节转化成一个整数 44 } 45 } 46 memset(Left, -1, sizeof(Left)); 47 int ans = 0; 48 for (int i = 1; i <= n; i++) 49 { 50 memset(vis, 0, sizeof(vis)); 51 if (mach(i)) 52 ans++; 53 } 54 printf("%d ", ans); 55 } 56 return 0; 57 }