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. iOS开发 编辑框被系统弹出的软键盘遮挡问题

    我们在开发注冊界面的时候,最后几个注冊条件经常easy被系统弹出的键盘遮挡,例如以下图: 能够看见,邮箱条件被遮挡掉了,怎么解决呢?我是通过UITextField的代理加计算偏移量: - (void) ...

  2. WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(二)实现IRequestChannel(2016-03-15 12:35)

    这是这个系列的第二篇,其他的文章请点击下列目录 WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(一)概要设计 WCF扩展之实现ZeroMQ绑定和protocolBuffer消息 ...

  3. cocos2d-x学习过程中的疑问

    1.一个Scene中不同的层或者有几层Layer是在什么时候设置的? 2.helloWord中init()函数是有谁来调用的? 答:HelloWorld的init函数是在create函数调用后才会调用 ...

  4. 怎么样Eclipse IDE for C/C++ Developers正确编译GTK规划?(解决)

    <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.99 ...

  5. web:转盘抽奖

    移动web:转盘抽奖(幸运大转盘)   为了获取客户.回馈客户,平台一般会推出抽奖活动类的营销页.因此web页面中,有各式各样的抽奖效果. 格子式(九宫格),背景滚动式(数字/文字/图案),旋转式(转 ...

  6. rsync+inotify 实现资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端

    测试环境: 资源服务器(主服务器):192.168.200.95 备份服务器(客户端):192.168.200.89 同步目录:/etc/test 同步时使用的用户名hadoop密码12345 实验目 ...

  7. iis虚拟目录引发的路径问题

    在iis上把web程序配置成站点是ok的,但配置成虚拟目录,就会发现 图片路径不能,样式不能加载,链接出错. 解决方案: 1,上传图片  ~/upload 2,cs程序,链接跳转,请用~/index. ...

  8. 学习swift语言的快速入门教程推荐

    随着苹果产品越来越火爆,苹果新推出的swift必定将在很大程度上代替oc语言.学好swift语言,对于IOS工程师来讲,已经是一门必备技能. 有一些比较好的英文版教程,值得学习. 1. Swift T ...

  9. 具体的了解“&gt;/dev/null 2&gt;&amp;1”

    Linux系统中不管是crontab里面.还是平时使用的命令.常常会碰到">/dev/null 2>&1".比方说:在Crontab Job里面,假设不想发送邮 ...

  10. JFinal 的源代码超具体的分析DB+ActiveRecord

    我记得有人告诉我."面试一下spring源代码.看ioc.aop源代码"那为什么要看这些开源框架的源代码呢,事实上非常多人都是"应急式"的去读.就像读一篇文章一 ...