题意:
给出N个集合。每次你可以指定一个数,然后所有包含这个元素的集合可以被删掉。
问你能否经过最多L轮操作使得所有集合都被删掉。
Sample Input
2
2 1
1 1
1 2
3 2
3 1 2 3
3 4 5 6
3 2 5 6
Sample Output
NO
YES
给出N个集合。每次你可以指定一个数,然后所有包含这个元素的集合可以被删掉。
问你能否经过最多L轮操作使得所有集合都被删掉。
Sample Input
2
2 1
1 1
1 2
3 2
3 1 2 3
3 4 5 6
3 2 5 6
Sample Output
NO
YES
范围很小,直接暴力;
# include <stdio.h>
# include <math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int mp[35][15],n,l;
bool used[305];//标记删除的数字
bool judge(int x)//判断该集合是否已被删除
{
for(int j=1; j<=mp[x][0]; j++)
if(used[mp[x][j]])
return true;
return false;
}
bool dfs(int x,int y)//x为第几个集合,y为删除操作的次数
{
if(x>=n) return true;
if(judge(x)) return dfs(x+1,y);//该集合已被删除,直接搜索下一个,删除操作次数不变;
if(y>=l) return false;
for(int i=1; i<=mp[x][0]; i++)
{
used[ mp[x][i] ] = true;
if( dfs(x+1,y+1) ) return true;
used[ mp[x][i] ] = false;
}
return false;
}
void test()
{
scanf("%d%d",&n,&l);
for(int i=0; i<n; i++)
{
scanf("%d",&mp[i][0]);
for(int j=1; j<=mp[i][0]; j++)
scanf("%d",&mp[i][j]);
}
memset(used,false,sizeof(used));
printf(dfs(0,0)?"YES\n":"NO\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
test();
return 0;
}