POJ2443

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2679   Accepted: 1050

Description

You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set" isn't entirely the same as the "set" defined in mathematics, and a set may contain two same element). Every element in a set is represented by a positive number from 1 to 10000.
Now there are some queries need to answer. A query is to determine whether two given elements i and j belong to at least one set at the same time. In another word, you should determine if there exist a number k (1 <= k <= N) such that element i belongs to
S(k) and element j also belong to S(k).

Input

First line of input contains an integer N (1 <= N <= 1000), which represents the amount of sets. Then follow N lines. Each starts with a number C(i) (1 <= C(i) <= 10000), and then C(i) numbers, which are separated with a space, follow to give the element in
the set (these C(i) numbers needn't be different from each other). The N + 2 line contains a number Q (1 <= Q <= 200000), representing the number of queries. Then follow Q lines. Each contains a pair of number i and j (1 <= i, j <= 10000, and i may equal to
j), which describe the elements need to be answer.

Output

For each query, in a single line, if there exist such a number k, print "Yes"; otherwise print "No".

Sample Input

3
3 1 2 3
3 1 2 5
1 10
4
1 3
1 5
3 5
1 10

Sample Output

Yes
Yes
No
No

Hint

The input may be large, and the I/O functions (cin/cout) of C++ language may be a little too slow for this problem.

Source

POJ Monthly,Minkerui

主要是简单的状态压缩,由于数字最大就是10000,而int最大是32位不到,所以切割成10000/30个,分成40份好了。

之后进行状态压缩,奇妙的二进制啊。

经过这样的处理后,插入是常数,而推断一个数是否在某一个集合也是常数时间。所以每个查询推断他是否在集合中也就是O(n)。

典型的空间换时间。

一開始我开了一个1000*10000的数组。memset斗能够导致TLE了。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct st{
int u[400];
void add(int n){
int i=n/30,j=n%30;
u[i]|=(1<<j);
}
bool in(int n){
int i=n/30,j=n%30;
return u[i]&(1<<j); }
void del(int n){
int i=n/30,j=n%30;
u[i]&=~(1<<j); }
void init(){
memset(u,0,sizeof(u));
} };
st v[1001];
int main()
{ int n,m,i,s,t;
while(scanf("%d",&n)!=EOF){ for(i=0;i<n;i++){
v[i].init(); scanf("%d",&m);
while(m--){
scanf("%d",&s);
v[i].add(s);
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d",&s,&t);
for(i=0;i<n;i++){
if(v[i].in(s)&&v[i].in(t)){ break;
}
}
if(i==n){
puts("No");
}else{
puts("Yes");
}
} }
return 0;
}

poj2443(简单的状态压缩)的更多相关文章

  1. cf1051d 简单的状态压缩dp

    /* 给定一个二行n列的格子,在里面填黑白色,要求通过黑白色将格子分为k块 请问有多少种填色方式 dp[j][k][0,1,2,3] 填到第j列,有k块,第j列的颜色, */ #include< ...

  2. [USACO06NOV]玉米田Corn Fields(动态规划,状态压缩)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  3. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  4. POJ2686 Traveling by Stagecoach 状态压缩DP

    POJ2686 比较简单的 状态压缩DP 注意DP方程转移时,新的状态必然数值上小于当前状态,故最外层循环为状态从大到小即可. #include <cstdio> #include < ...

  5. POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)

    推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...

  6. 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举

    题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...

  7. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  8. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  9. poj3254 状态压缩dp

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法.     分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...

随机推荐

  1. Java Map 迭代

    Map迭代 有两种 道路 遍历 Map该方法:      1  Set<K> KeySet(): 获取全部的键,得到set集合,迭代, 通过get( key)获取值!      2  Se ...

  2. Java调用Lua(转)

    Java 调用 Lua app发版成本高,覆盖速度慢,覆盖率页低.一些策略上的东西如果能够从服务端控制会方便一些.所以考虑使用Lua这种嵌入式语言作为策略实现,Java则是宿主语言. 总体上看是一个模 ...

  3. CSS+DIV+HTML(一)--HTML总结

    一.定义 HTML(Hyper Text Markup Language),标记语言. 二.主要内容: HTML元素分为三类:块级标签.内联标签.可变标签.差别在于: 块级元素:在默认情况下会换行显示 ...

  4. c#操作appsettiongs

    try { //指定要修改的配置文件的路径 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWe ...

  5. Effective C++ Item 32 确保你的 public 继承模子里出来 is-a 关联

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:"public继承"意味 is-a.适用于 base classe ...

  6. 安装Microsoft .NET Framework 3.5 Service Pack 1回报1603错

    server升级了一下系统补丁(360安装),所有发现.net无法打开网站,提示" 因为无法创建应用程序域,因此未能运行请求.错误: 0x80070002 系统找不到指定的文件. " ...

  7. 经常使用Log日志打印输出

    /** * log刊物 * @author Jenly * */ public class LogUtils { public static final String TAG = "Jenl ...

  8. 他们主动布局(autolayout)环境的图像编辑器

    hi,all: 在经过了一番犹豫之后.我决定将我自己做的这个小APP的源代码发布给大家: 其出发点是和大家一起学习iOS开发.仅供学习參考之用. 之前代码是托管与gitlab 上的,今天我将其pull ...

  9. Could not drop object &#39;student&#39; because it is referenced by a FOREIGN KEY constraint

    1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...

  10. 如何从Terminal Command Line编译并运行Scope

    Ubuntu SDK我们大部分的开发者是非常有效的.它甚至可以帮助我们进行在线调试.在这篇文章中,我们介绍了如何使用command line编译和执行我们scope. 1)创建一个主Scope 我们能 ...