Problem Description
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.
 
Input
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.
 
Output
For each test print YES or NO
 
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

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[N];
int s[N];
int vis[M];
int flag;
bool judge(int num){
for(int i=;i<s[num];i++){
int val=g[num][i];
if(vis[val]){
return true;
}
}
return false;
}
void dfs(int num,int d){
if(num>=n){
flag=;
return;
}
if(judge(num)){
dfs(num+,d);
}
if(flag){
return;
} if(d>L){
return;
} for(int i=;i<s[num];i++){
int val=g[num][i];
vis[val]=;
dfs(num+,d+);
if(flag){
return;
}
vis[val]=;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&L);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<n;i++){
scanf("%d",&s[i]);
for(int j=;j<s[i];j++){
int c;
scanf("%d",&c);
g[i].push_back(c);
}
}
memset(vis,,sizeof(vis));
flag=;
dfs(,);
if(flag==){
printf("YES\n");
}else{
printf("NO\n");
} }
return ;
}

第二个代码,正确。

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

hdu 5506 GT and set(dfs爆搜)的更多相关文章

  1. HDU 4403 A very hard Aoshu problem(dfs爆搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...

  2. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  3. hdu 4770(枚举 + dfs爆搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部 ...

  4. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdu 5506 GT and set dfs+bitset优化

    GT and set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Probl ...

  6. 【BZOJ-1060】时态同步 树形DP (DFS爆搜)

    1060: [ZJOI2007]时态同步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2101  Solved: 595[Submit][Statu ...

  7. POJ3185 The Water Bowls(反转法or dfs 爆搜)

    POJ3185 The Water Bowls 题目大意: 奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝 ...

  8. hdu 4277 USACO ORZ (dfs暴搜+hash)

    题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...

  9. Ancient Go---hdu5546(dfs爆搜CCPC题目)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...

随机推荐

  1. WordPress SEO ☞ WordPress网站终极优化指南

    原文地址:http://www.eastdesign.net/wordpress-seo/ 最新消息,东方设计学院 WordPress SEO 系列视频教程正在持续更新中,目前为了不至于让视频传播过于 ...

  2. java 常用的验证方法帮助类

    import java.text.ParseException; import java.util.Collection; import java.util.Map; /** * 常用的验证方法帮助类 ...

  3. LeeCode-Pow(x, n)

    Implement pow(x, n). double myPow(double x, int n) { ) return 1.0; ) return 1.0/pow(x,-n); ); }

  4. mongoDB windows reinstall add auth

    Mongodb默认启动是不带认证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,这样可不行.现在,我们得一步步开启使用用户和认证. 第一步,我们得定位到mongodb的安装目录.我本机的是C ...

  5. hdu1556 Color the ball

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  6. javax.mail用smtp服务器发送带附件的邮件

    jar包: javax.mail-1.5.5.jar maven配置: <dependency> <groupId>com.sun.mail</groupId> & ...

  7. IOS 使用dispatch_once 创建单例

    + (instantClass *)sharedClient { static instantClass *_sharedClient = nil; static dispatch_once_t on ...

  8. 关于继承扩展ASP.NET控件(以Textbox为例)

    以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展. using System; using System.Collections.Generic; using System.Linq ...

  9. material design 图标制作参数

    可用图标的标准不透明度在亮色背景上是54%(#000000).可视等级较低的禁用图标的不透明度应为 26%(#000000). 可用图标的标准不透明度在暗色背景上是 100%(#FFFFFF).可视等 ...

  10. .NET 下成熟开源的BPM产品四款推荐

    .net下的BPM产品相比JAVA的确实不多,这里主要提4款. 1.博客园.github.codeplex上的开源的流程组件AppInOne BPM,目前已有不少的企业开始使用. 优点:产品框架较全面 ...