题目大意:
P个老师,N个学生,能否达到最大匹配P;
1 #include<bits/stdc++.h> 2 #define pb push_back 3 const int maxn = 300 + 5; 4 using namespace std; 5 int P,N; 6 vector<int> G[maxn]; 7 int linker[maxn]; 8 bool used[maxn]; 9 bool dfs (int u) { 10 int now ; 11 for ( int v = 0 ; v < G[u].size() ; v ++ ){ 12 now = G[u][v]; 13 if(!used[now]){ 14 used[now] = true ; 15 if( linker[now] == -1 || dfs(linker[now]) ){ 16 linker[now] = u; 17 return true ; 18 } 19 } 20 } 21 return false; 22 } 23 int hungary(){ 24 int res = 0; 25 memset( linker , -1 ,sizeof( linker )); 26 for ( int u = 1; u <= P ; u++) { 27 memset ( used , false ,sizeof (used )) ; 28 if( dfs(u)) res ++ ; 29 } 30 return res ; 31 } 32 int main () { 33 int T , tot , v ; 34 cin >> T; 35 while (T--) { 36 cin >> P >> N; 37 for ( int i = 1 ; i <= P ;i ++) { 38 cin >> tot ; 39 for ( int j = 1; j <= tot ; j++){ 40 cin >> v ; 41 G[i].pb (v); 42 } 43 } 44 printf ( "%s " , hungary() == P ? "YES" : "NO" ); 45 for ( int i = 1; i <= P ; i++ ) G[i].clear(); 46 } 47 return 0; 48 }