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. spring boot jar的支持

  2. HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 【LeetCode 90】子集 II

    题目链接 [题解] 我们在枚举下一个要取哪个数字的时候. 如 1112233 for (int i = start;i<=n;i++) //其中start-1是上一次取的位置. 如果i>s ...

  4. [BZOJ1572] WorkScheduling

    中文题目:工作安排 原文题目:Work Scheduling 传送门 本题可以采用贪心 算法一:按工作时间排序,如果工作能按时完成的工作就按时完成,如果工作不能按时完成就把之前价值最小的工作和当前作比 ...

  5. Oracle 简单统计示例

    有数据如下: eg1:现在需要统计所有男性人员数量,所有女性人员数量,sclassno=10000的男性人员的总年龄,年龄大于20的女性人员数量 ----sign( number )/*If numb ...

  6. JDK 与 JRE

    JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境.SDK是Software Development Kit 一般指软件 ...

  7. Jenkins构建触发器的区别

    Build periodically:定时进行项目构建或执行(它不care源码是否发生变化),配置如下: 0 2 * * *  (每天2:00 必须build一次源码) 如果是要定时执行脚本,需要选择 ...

  8. Red Hat 6网络配置笔记

    1.重启网卡/etc/init.d/network restart server network restart2.运行结果报错 关闭NetworkManagerservice NetworkMana ...

  9. js中Object.defineProperty()方法的解释

    菜菜: “老大,那个, Object.defineProperty 是什么鬼?” 假设我们有个对象 user ; 我们要给它增加一个属性 name , 我们会这么做 1 2 3 var user = ...

  10. json模块 pickle 模块 collections 模块 openpyxl 模块

    json模块 json 模块是一个系列化模块 一个第三方的特殊数据格式 可以将python数据类型----> json 数据格式 ----> 字符串 ----> 文件 其他语言想要使 ...