GT and set

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

 
Source
 题意:如果n个集合的交集不为空就可以合并,问n个能合并为L个;
思路:暴力搜索,两个集合合并的话用bitset优化即可;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
bitset<> flag[],a[],temp,bit;
int n,m,ans;
void dfs(int pos)
{
if(pos>n||ans)
{
ans=;
return;
}
for(int i=;i<=m;i++)
{
temp=flag[i]&a[pos];
bit=flag[i];
if(temp.count())
{
flag[i]=temp;
dfs(pos+);
flag[i]=bit;
}
}
}
void init()
{
for(int i=;i<=n;i++)
a[i].reset();
for(int i=;i<=m;i++)
flag[i].set();
ans=;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;i++)
{
int z;
scanf("%d",&z);
for(int j=;j<=z;j++)
{
int x;
scanf("%d",&x);
a[i].set(x);
}
}
dfs();
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

hdu 5506 GT and set dfs+bitset优化的更多相关文章

  1. HDU 5808 Price List Strike Back bitset优化的背包。。水过去了

    http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...

  2. 洛谷P1441 砝码称重(搜索,dfs+bitset优化)

    洛谷P1441 砝码称重 \(n\) 的范围为 \(n \le 20\) ,\(m\) 的范围为 \(m \le 4\) . 暴力遍历每一种砝码去除情况,共有 \(n^m\) 种情况. 对于剩余砝码求 ...

  3. HDU 5506:GT and set bitset+暴力

    GT and set  Accepts: 35  Submissions: 194  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

  4. HDU 5890 Eighty seven(DP+bitset优化)

    题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...

  5. Hdu 6268 点分治 树上背包 bitset 优化

    给你一颗大小为n(3000)的树,树上每个点有点权(100000),再给你一个数m(100000) i为1~m,问树中是否存在一个子图,使得权值为i. 每次solve到一个节点 用一个bitset维护 ...

  6. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  7. HDU - 6268: Master of Subgraph (分治+bitset优化背包)

    题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包 ...

  8. hdu 5036 Explosion bitset优化floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...

  9. hdu 4109 dfs+剪枝优化

    求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...

随机推荐

  1. Javascript Promise入门

    是什么? https://www.promisejs.org/ What is a promise? The core idea behind promises is that a promise r ...

  2. 把 MWeb Lite 的文档库文档和数据搬到 MWeb 正式版中

    MWeb Lite 版的文档库中的文档要搬到 MWeb 正式版中,如果 Lite 版的文档中没有图片或者只有少量图片,可以用导入导出为 Markdown 的方法. 否则的话请用以下方式(注意下面这个方 ...

  3. 总结Android中遇见的OOM

    一 .Android应用中内存泄漏几种的原因: 1.单例模式导致的内存泄漏: 当调用getInstance时,如果传入的context是Activity的context.只要这个单例没有被释放,这个A ...

  4. css名词解释

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. java类

    1.四种访问控制符 private.不指明.protected.public 2.构造函数没有返回值 是因为构造函数默认返回的是类的实例对象.所以不需要写返回值 3.static static表示类拥 ...

  6. oracle学习笔记

    --Oracle查询当前版本select * from v$version;----------Oracle 查询服务器端编码----------select * from v$nls_paramet ...

  7. 关于header跳转之后的乱码

    http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同编码的页面传递参数也出现乱码 搞清楚 ...

  8. 转载 wpf使用经验

    转载自 胡庆访[ http://zgynhqf.cnblogs.com/ ] WPF 是一个界面层框架技术,要对 WPF 技术达到熟练运用的程度,需要同时拥有开发和设计两方面的知识.而我作为一名开发人 ...

  9. Array常用方法

    定义二维数组: list = [['保密',''],['男',1],['女',0]] 引用 怎么创建与返回值是二维数组形式 不知道你想要怎样的答案 如果是一方法想要返回二维数组,方法的最后一行是那个数 ...

  10. cherrypy应用探究

    1. cherrypy是什么? cheerypy是一个有pythonic特性的面向对象的http服务框架. 玩python的人都应该知道pythonic这个单词.python大神给我们的建议 : &g ...