数据弱......直接枚举就行......
DFS(n,m)表示加入第n个人,此时用了m个球,root[n] = i表示第n哥人属于第i个人那一拨。
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int N, M;
bool map[10][10];
int root[11];
bool finish;
void DFS( int n, int m ){
if( m > M ){
return;
}
if( finish ){
return;
}
if( n == N ){
finish = true;
return;
}
for( int i = 0; i < n; i++ ){
if( root[i] != i ){
continue;
}
bool flag = true;
for( int j = 0; j < n && flag; j++ ){
if( root[j] == i ){
flag = map[n][j];
}
}
if( flag ){
root[n] = i;
DFS( n + 1, m );
root[n] = n;
}
}
DFS( n + 1, m + 1 );
}
int main(){
while( scanf( "%d%d", &N, &M ) != EOF ){
memset( map, false, sizeof( map ) );
for( int i = 0; i < N; i++ ){
root[i] = i;
int temp;
cin >> temp;
for( int j = 0; j < temp; j++ ){
int temp1;
cin >> temp1;
map[i][temp1] = map[temp1][i] = true;
}
}
finish = false;
DFS( 0, 0 );
if( finish || N <= M ){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
return 0;
}