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. 如何在MaxCompute上处理存储在OSS上的开源格式数据

    0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...

  2. nil,Nil,NULL和NSNull的区别

  3. PHP curl_multi_close函数

    curl_multi_close — 关闭一组cURL句柄 说明 void curl_multi_close ( resource $mh ) 关闭一组cURL句柄. 参数 mh 由 curl_mul ...

  4. Visual Studio 2008 附加进程调试

    关于附加进程调试的问题: 在项目当中经常使用“附加到进程”来调试项目,感觉挺方便的.我们做的项目通常都会发布到IIS(特别是B/S),一可以直接通过地址栏输入地址就可以运行项目,不必去使用开发工具来打 ...

  5. 23-25 October in 614

    Practice sort 给定一系列形如 \(A<B\) 的不等关系,判断前 \(k\) 个不等关系是否即可确定 \(n\) 个元素之间的大小顺序:如果不可确定,判断前 \(k\) 个不等关系 ...

  6. 父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响

    原文地址 背景 开发过程中遇到问题,简单写个demo 运行环境为Chrome 68 描述一下这个问题,当a标签内部存在嵌套时, 父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存 ...

  7. DIV置底层或置最高层的方法下拉菜单被挡住

    网站常会用到一些 下拉菜单,,幻灯片,,,飘浮广告等. 但经常会发现.幻灯片会挡住下拉菜单或者飘浮广告微信开店等. 解决办法有下 第一,可将幻灯片所在DIV 置于最底层.添加CSS如下 style=& ...

  8. 精简Docker镜像的几个方法

    一.使用更精简的镜像 常用的Linux系统镜像一般有 Debian.Ubuntu.CentOS和Alpine,其中Alpine是面向安全的轻量级Linux发行版本.Docker的Alpine镜像仅有不 ...

  9. 关于html 修改滚动条的问题

    之前项目需要改变滚动条的样式 一.修改原生样式 原文地址:https://blog.csdn.net/zh_rey/article/details/72473284 问题在于无法兼容火狐与ie等浏览器 ...

  10. Mybatis+Springmvc+Spring整合常用的配置文件

    1.创建web项目 2.导入mabatis spring springnvc 需要的jar包 3.创建mybatis,spring,springmvc的配置文件 (1)web.xml配置文件 < ...