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 给你N(1<=N<=1000)个集合,每个集合里面有不超过10000个可以重复的元素
给你不超过10000次的询问,每个询问给你两个数,问有没有一个集合同时包含这两个数
思路:按照位进行压缩,因为对与一个数是否在集合中只有在或者不在两种情况于是用一个二进制位存就够了
这样大大节省了空间,一个int是32位,询问的数字最大是10000.我们按照30个一组的分块,一共大概要分400块
这很类似与硬盘的分块...然后我们用位运算维护更新下每一个集合,对于每个询问我们去枚举每一个集合就可以了
 #include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int BufferSize=<<;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
const int maxn = ;
const int Size = ;
int n,m,q;
struct Set
{
int st[];
void init (){for (int i=;i<;++i) st[i]=;}
int getBlk(int num){return num/Size;}
int getIdx(int num){return num%Size;}
void Add (int x){st[getBlk(x)]|=(<<getIdx(x));}
void Del (int x){st[getBlk(x)]&=~(<<getIdx(x));}
bool In (int x){return st[getBlk(x)]&(<<getIdx(x));}
};
Set a[maxn];
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n)){
for (int i=;i<maxn;++i)
a[i].init();
for (int i=;i<n;++i){
m=read();
for (int j=;j<m;++j){
int x;
x=read();
a[i].Add(x);
}
}
q=read();
for (int i=;i<q;++i){
int x,y;
x=read();y=read();
bool f=false;
for (int i=;i<n;++i){
if (a[i].In(x)&&a[i].In(y)){
f=true;break;
}
}
if(f) printf("Yes\n");
else printf("No\n");
}
}
return ;
}

POJ 2443 Set Operation (按位压缩)的更多相关文章

  1. POJ 2443 Set Operation(压位加速)

    http://poj.org/problem?id=2443 题意: 有1000个集合,每个集合有至多10000个数,之后输入多个询问,判断询问的两个数是否位于同一个集合. 思路: 位运算...很强大 ...

  2. [POJ 2443] Set Operation (bitset)

    题目链接:http://poj.org/problem?id=2443 题目大意:给你N个集合,每个集合里有若干个数.M个查询,每个查询有a,b两个数.问是否存在一个集合同时包含a,b这两个数.若存在 ...

  3. POJ 2443 Set Operation

    Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3558   Accepted: 1479 Des ...

  4. POJ 2443 Set Operation 题解

    本文同时发布于 博客园 洛谷博客 题目链接 题目分析 给你n个集合,每个集合里面都有可能会重复的数字 q个询问,每次询问两个数是否会在同一集合内 $n<=1000$ $q<=200000$ ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  7. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  8. scala位压缩与行情转换二进制

    import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer} import java.nio.charset.Charset import ...

  9. POJ 2443:Set Operation 经典位运算好题

    Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2965   Accepted: 1196 Des ...

随机推荐

  1. objc_setAssociatedObject 关联对象

    使用场景:在分类中,不允许创建实例变量,这里就解决了此问题 参考: https://www.cnblogs.com/someonelikeyou/p/7162613.html 属性的实质:就是实例变量 ...

  2. php strnatcmp()函数 语法

    php strnatcmp()函数 语法 作用:自然顺序法比较字符串直线往复电机 语法:strnatcmp(string1,string2) 参数: 参数 描述 string1 必须,规定要比较的第一 ...

  3. 【LeetCode 84】柱状图中最大的矩形

    题目链接 [题解] 维护一个单调递增的栈. 会发现栈内的第i个元素的前面一个(i-1)元素在原始的序列中的数字 都是要高于第i个元素的.(或者没有元素) 那么第i个元素往左最多可以扩展到第i-1个元素 ...

  4. cf 1263

    A #include<bits/stdc++.h> using namespace std; int main(){ int t;cin>>t; while(t--){ ]; ...

  5. 本地项目关联到远程git仓库

    本地项目关联到远程git仓库 场景是这样的:在gitee-code上新建一个项目,gitee会帮你把项目初始化,初始化之后会根据你的选择生成默认的说明文件和gitignore文件.这个时候你就可以复制 ...

  6. [CSP-S模拟测试]:序列(主席树)

    题目描述 小$A$把自己之前得到的序列展示给了小$B$,不过这一次,他并不要求小$B$模仿他之前的行为.他给了小$B$一些询问,每个询问都是$l\ r\ x$的形式,要求小$B$数出在序列的第$l$个 ...

  7. 听说你懂个J?——前端发展闲聊

    刚好周末和朋友聊起"前端从受鄙视到变得重要"这个话题,感慨前端这四年来的发展,遂有本文. 1. 前情提要 毋庸讳言,在我刚工作的时候,前端是还是一个不受重视的岗位.切图狗,写网页的 ...

  8. openoffice+pdf2swf+FlexPaper在线显示office和pdf

    前提:本人的系统为Ubuntu 13.10 64位系统.本篇是我在配置好环境后一段时间写的,所以操作上可能会有也错误,因此仅供参考. 搜索在线显示office和pdf,最常见的方法就是把都转为swf, ...

  9. Nginx允许跨域访问的配置问题

    网站项目动静分离,静态资源服务器A 业务服务器B  B中静态资源由A加载 出现如下问题: @font-face { font-family: 'iconfont'; src: url('../font ...

  10. git 多个远程仓库

    有时候一个git项目需要使用多个远程库,如:测试环境+生产环境,国内加国外等 项目根目录下修改 .git/config 文件 vim .git/config 新增远程一个远程仓库   并为其命名 :如 ...