二分匹配,匈牙利算法
// File Name: 1186.cpp
// Author: bo_jwolf
// Created Time: 2014年02月06日 星期四 23时09分26秒
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
const int maxn = 1000;
int uN, vN;
int g[ maxn ][ maxn ], linker[ maxn ], vis[ maxn ];
bool Dfs( int u ){
for( int v = 1; v <= vN; ++v ){
if( g[ u ][ v ] && !vis[ v ] ){
vis[ v ] = true;
if( linker[ v ] == -1 || Dfs( linker[ v ] ) ){
linker[ v ] = u;
return true;
}
}
}
return false;
}
bool hungary(){
int res = 0;
memset( linker, -1, sizeof( linker ) );
for( int u = 1; u <= uN; ++u ){
memset( vis, 0, sizeof( vis ) );
if( !Dfs( u ) ){
res++;
//return false;
}
}
return res;
}
int main(){
int Case, temp, t;
cin >> Case;
while( Case-- ){
cin >> uN >> vN;
memset( g, 0, sizeof( g ) );
for( int i = 1; i <= uN; ++i ){
cin >> temp;
while( temp-- ){
cin >> t;
g[ i ][ t ] = true;
}
}
if( !hungary() ){
puts( "YES" );
}
else{
puts( "NO" );
}
}
return 0;
}