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. php chunk_split()函数 语法

    php chunk_split()函数 语法 作用:把字符串分割为一连串更小的部分.东莞大理石平板 语法:chunk_split(string,length,end) 参数: 参数 描述 string ...

  2. Git中的分支

    具体请参考:https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E4%BD%95%E8%B0%93%E5%88%86%E6%94%AF Git ...

  3. 十、future其他成员函数、shared_future、atomic(原子操作)

    一. int mythread(){ cout<<"thread"<<endl; std::chrono::milliseconds dura();//5秒 ...

  4. window安装oracle和创建数据库

    原文地址: https://www.cnblogs.com/hoobey/p/6010804.html  #11g安装 https://www.cnblogs.com/qq1272850043/p/6 ...

  5. mysql中or和in,in和exists的效率问题

     mysql中or和in的效率问题      在网上一直看到的是or和in的效率没啥区别,一直也感觉是这样,前几天刚好在看<mysql数据库开发的36条军规>的文章,里面提到了or和in的 ...

  6. Asynchronous C# server[转]

    It hasn't been thoroughly tested, but seems to work OK. This should scale pretty nicely as well. Ori ...

  7. linux svn 服务器搭建问题

    我的svn版本 svn, version 1.7.14 (r1542130) compiled Nov 20 2015, 19:25:09 Copyright (C) 2013 The Apache ...

  8. CSS属性去除图片链接时的虚线框

    CSS 之outline (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用.outline 属性是一个简写属性,用于设置元素周围的轮廓线.注释:轮廓线不会占据空间,也不一定是 ...

  9. printf输出各种类型,cout控制输出各式

    ; char c = 'A'; int *p = &a; char *st = "ahj"; float x = 3.1415926; cout << & ...

  10. Numpy的基本运算及操作

    import numpy as np ''' 一.算术运算 元素级 1.标量 加减乘除 数组(元素级:位置对应) 自增和自减 通用函数 2.数组 +-*/ 数组 (元素级) 3.条件和布尔运算 a&g ...