您的位置:首页 > 其它

hdu 5506 GT and set(dfs爆搜)

2016-01-26 02:08 246 查看
[align=left]Problem Description[/align]
You are given N sets.The i−th set has Ai numbers.
You should divide the sets into L parts.
And each part should have at least one number in common.
If there is at least one solution,print YES,otherwise print NO.

[align=left]Input[/align]
In the first line there is the testcase T (T≤20)
For each teatcase:
In the first line there are two numbers N and L.
In the next N lines,each line describe a set.
The first number is Ai,and then there are Ai distict numbers stand for the elements int the set.
The numbers in the set are all positive numbers and they're all not bigger than 300.
1≤N≤30,1≤L≤5,1≤Ai≤10,1≤L≤N
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.

[align=left]Output[/align]
For each test print YES or NO

[align=left]Sample Input[/align]

2
2 1
1 1
1 2
3 2
3 1 2 3
3 4 5 6
3 2 5 6

[align=left]Sample Output[/align]

NO
YES

Hint

For the second test,there are three sets:{1,2,3},{4,5,6},{2,5,6}
You are asked to divide into two parts.
One possible solution is to put the second and the third sets into the same part,and put the first in the other part.
The second part and the third part have same number 6.
Another solution is to put the first and the third sets into the same part,and put the second in the other part.

题意:

有n个集合,问你能否在L次内把所有集合都删去,如果两个或者更多集合内含有一个相同的数,则这些集合可以同时删除

每个集合中的元素小于10,L<=5,n<=30

分析:

由于数据范围都比较小,很容易想到搜索

很容易想到可以枚举当前集合应该删除哪个数,如果下一个集合中已经出现过了这个数,则跳过下一个集合,可以选择的删除的数不超过5个

每个集合中最多有10个数能被选择,所以时间复杂度也就为10^5*n,乘以n是因为要判断需不需要从当前这个集合中删除一个数

首先贴上第一个代码,这个代码在oj上能AC,但是题目给出的样例中的第一个样例却过不来。和下面贴出的第二个样例写得差不多,但就是样例有一个过不了,不懂什么原因。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 36
#define M 306
#define inf 1e12
int n,L;
vector<int> g
;
int s
;
int vis[M];
int flag;
bool judge(int num){
for(int i=0;i<s[num];i++){
int val=g[num][i];
if(vis[val]){
return true;
}
}
return false;
}
bool dfs(int num,int d){

if(num>=n){
//flag=1;
return true;
}
if(judge(num)){
return dfs(num+1,d);
}
//if(flag) return;
if(d>=L) return false;
for(int i=0;i<s[num];i++){
int val=g[num][i];
vis[val]=1;
if(dfs(num+1,d+1)) return true;
//if(flag){
//    return;
//    }
vis[val]=0;
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&L);
for(int i=0;i<=n;i++){
g[i].clear();
}
for(int i=0;i<n;i++){
scanf("%d",&s[i]);
for(int j=0;j<s[i];j++){
int c;
scanf("%d",&c);
g[i].push_back(c);
}
}
memset(vis,0,sizeof(vis));
/*flag=0;
dfs(0,0);
if(flag==1){
printf("YES\n");
}else{
printf("NO\n");
}
*/
printf("%s\n", dfs(0, 0) ? "YES" : "NO");

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: