题意:N个学生,P个课程,问能不能找到课程的P个匹配。
思路:【早上睡醒了再写】
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 const int maxn = 555; 7 int n, p; 8 vector<int> g[maxn]; 9 int from[maxn], tot; 10 bool use[maxn]; 11 12 bool match(int x) 13 { 14 for (int i = 0; i < g[x].size(); i ++) { 15 if(!use[g[x][i]]) { 16 use[g[x][i]] = true; 17 if(from[g[x][i]] == -1 || match(from[g[x][i]])) { 18 from[g[x][i]] = x; 19 return true; 20 } 21 } 22 } 23 return false; 24 } 25 26 int hungary() 27 { 28 tot = 0; 29 memset(from, -1, sizeof(from)); 30 for(int i = 1; i <= n; i ++) { 31 memset(use, 0, sizeof(use)); 32 if(match(i)) tot ++; 33 } 34 return tot; 35 } 36 37 int main() 38 { 39 int t; cin>>t; 40 while(t--) { 41 for(int i = 0; i < maxn; i++) g[i].clear(); 42 scanf("%d%d", &p, &n); 43 for(int i = 1; i <= p; i++) { 44 int k; scanf("%d", &k); 45 for(int j = 0; j < k; j++) { 46 int a; scanf("%d", &a); 47 g[i].push_back(a); 48 } 49 } 50 int res = hungary(); 51 //cout<<res<<endl; 52 if(res == p) printf("YES "); 53 else printf("NO "); 54 } 55 }