hdu 5506 GT and set dfs+bitset优化

时间:2022-09-18 20:17:58

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&plus;bitset优化)

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

  3. HDU 5506:GT and set bitset&plus;暴力

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

  4. HDU 5890 Eighty seven(DP&plus;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 &plus; bitset优化

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

  7. HDU - 6268: Master of Subgraph (分治&plus;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&plus;剪枝优化

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

随机推荐

  1. 【转】搞不清FastCgi与php-fpm之间是个什么样的关系?

    我在网上查fastcgi与php-fpm的关系,查了快一周了,基本看了个遍,真是众说纷纭,没一个权威性的定义. 网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-f ...

  2. 【代码笔记】iOS-两个滚动条,上下都能滑动

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  3. 微分方程&mdash&semi;&mdash&semi;基本概念和常微分方程的发展史

    1.2 基本概念和常微分方程的发展史 自变量.未知函数均为实值的微分方程称为实值微分方程:未知函数取复值或变量及未知函数均取复值时称为复值微分方程.若无特别声明,以下均指实变量的实值微分方程. 1.2 ...

  4. ASP&period;NET WebForm与MVC优缺点

    发表于我的个人网站中,请点击阅读!

  5. 解析XML的四种方式

    四种操作xml的方式: SAX, DOM, JDOM , DOM4J的比较 1. 介绍 1)DOM(JAXP Crimson解析器)         DOM是用与平台和语言无关的方式表示XML文档的官 ...

  6. nginx第三方模块---nginx-sticky-module的使用(基于cookie的会话保持)

    目前的项目网站架构中使用了F5和nginx,F5用来做负载均衡,nginx只用作反向代理服务器.最近应客户的要求准备去掉F5,使用软负载.大家都知道nginx抗并发能力强,又可以做负载均衡,而且使用n ...

  7. 网络传输编程之TCP

    网络传输编程之TCP   网络传输编程指基于各种网络协议进行编程,包括TCP编程,UDP编程,P2P编程.本节介绍TCP编程.     (1)TCP简介: TCP是TCP/IP体系中最重要的传输层协议 ...

  8. 安装freemarker模板的ftl插件

    安装freemarker模板的ftl插件 同意协议 等待运行完成 重新启动eclipse 查看是否生效

  9. FM与PM信号的表现形式

    角度调制可以写成如下形式: $u(t)=A_c cos(2\pi f_c t + \phi (t) )$ $A_c cos(2\pi f_c t)$是载波,调制信号控制$\phi (t)$. 对于PM ...

  10. ASP&period;NET Core 2 学习笔记(十四)Filters

    Filter是延续ASP.NET MVC的产物,同样保留了五种的Filter,分别是Authorization Filter.Resource Filter.Action Filter.Excepti ...