题意:题目大意:将vy个工作分配给vx个学生,求能不能每个同学都有分配到工作。
解题思路:二分图匹配,看匹配数是否等于课程数
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 305;
const int P = 105;
int T[N], line[N], w[P][N];
int p, n;
bool find(int u){
for ( int i=1; i<=n; i++ )
if( !T[i] && w[u][i] ){
T[i]=1;
if( !line[i] || find(line[i]) ){
line[i]=u;
return true;
}
}
return false;
}
int Hungarian(){
int ans=0;
memset(line,0,sizeof(line));
for ( int i=1; i<=p; i++ ){
memset(T,0,sizeof(T));
if( find(i) ) ans++;
}
return ans;
}
int main(){
int tt;
scanf("%d", &tt );
while( tt-- ){
memset(w,0,sizeof(w));
scanf("%d%d", &p, &n );
for ( int i=1; i<=p; i++ ){
int x;
scanf("%d", &x );
for ( int j=1; j<=x; j++ ){
int v;
scanf("%d", &v );
w[i][v]=1;
}
}
(Hungarian()==p) ? puts("YES") : puts("NO");
}
}