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.

题意:给定N个集合,Q次询问,对每次询问,求X,Y是否在同一集合出现过,注意X=Y时,X在一个集合里至少出现一次就满足了。

思路:用Bitset来表示每个元素在哪些集合出现过,如果X和Y出现的集合有交集,则满足。

积累一下:

s.set()是全部置为1,s.set(pos)是某一位置为1,s.reset()是全部置为0.

s.flip(),按位取反。

#include<cstdio>
#include<bitset>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<>s[maxn+];
int read()
{
char c=getchar(); int res;
while(c>''||c<'') c=getchar();
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
return res;
}
int main()
{
int N,Q,i,num,x,y;
while(~scanf("%d",&N)){
for(i=;i<=maxn;i++) s[i].reset();
for(i=;i<=N;i++){
num=read();
while(num--){
x=read();
s[x][i]=; //s[x].set(i);
}
}
Q=read();
while(Q--){
x=read(); y=read();
if((s[x]&s[y]).count()) puts("Yes"); //.any()
else puts("No");
}
}
return ;
}

POJ2443 Set Operation (基础bitset应用,求交集)的更多相关文章

  1. [POJ2443]Set Operation(bitset)

    传送门 题意:给出n个集合(n<=1000),每个集合中最多有10000个数,每个数的范围为1~10000,给出q次询问(q<=200000),每次给出两个数u,v判断是否有一个集合中同时 ...

  2. POJ2443 Set Operation —— bitset

    题目链接:https://vjudge.net/problem/POJ-2443 Set Operation Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  3. Redis实现求交集操作结果缓存的设计方案

    Redis的集合操作 实话说,Redis提供的集合操作是我选择它成为内存数据库的一个主要理由,它弥补了传统关系型数据库在这方面带来的复杂度,使得只需要简单的一个命令就可以完成一个复杂SQL任务,并且交 ...

  4. 【转载】C#编程中两个List集合使用Intersect方法求交集

    在C#语言程序设计中,List集合是常用的集合数据类型,在涉及集合类型的运算中,有时候我们需要计算2个List集合中共有的数据,即对2个List集合求交集运算.此时可以使用C#语言提供的Interse ...

  5. for循环求交集

    for循环方式求交集 #!/usr/bin/env python #coding:utf-8 #取交集 #定义两个序列对象,求alist与blist对象的交集元素 alist = [2,9,3,4,5 ...

  6. PHP实现 bitmap 位图排序 求交集

    2014年12月16日 17:15:09 初始化一串全为0的二进制; 现有一串无序的整数数组; 如果整数x在这个整数数组当中,就将二进制串的第x位置为1; 然后顺序读取这个二进制串,并将为1的位转换成 ...

  7. [ECNU 1624] 求交集多边形面积

    求交集多边形面积 Time Limit:1000MS Memory Limit:30000KB Total Submit:98 Accepted:42 Description 在平面上有两给定的凸多边 ...

  8. python list求交集

    方法一: a=[1,2,3] b=[1,3,4] c=list(set(a).intersection(set(b))) print c #[1,3] 这种方法是先把list转换为set,再用set求 ...

  9. javascript集合求交集

    两集合求交集 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以 ...

  10. python 两个list 求交集,并集,差集

    def diff(listA,listB): #求交集的两种方式 retA = [i for i in listA if i in listB] retB = list(set(listA).inte ...

随机推荐

  1. Netty构建游戏服务器(三)--netty spring简单整合

    一,基本方法 上节实现了netty的基本连接,这节加入spring来管理netty,由spring来开启netty服务. 在netty服务器中,我们建立了三个类:HelloServer(程序主入口) ...

  2. Executors

    提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...

  3. Delphi GDI对象之绘制文本

    转载:http://www.cnblogs.com/pchmonster/archive/2012/07/06/2579185.html 基本绘图操作(Basic Drawing Operations ...

  4. Zabbix监控Mongo

    安装Zabbix-agent # groupadd zabbix # useradd -g zabbix zabbix # yum -y install gcc mysql-community-dev ...

  5. Android6.0权限管理以及使用权限该注意的地方

    Android 6.0 Marshmallow首次增加了执行时权限管理,这对用户来说,能够更好的了解.控 制 app 涉及到的权限.然而对开发人员来说却是一件比較蛋疼的事情.须要兼容适配,并保证程序功 ...

  6. apache多网站配置

    前言  虽说apache安装好后给了我们一个默认的一个网站.并且我们还能够将这个默认的网站改动成我们自己的网站.可是这似乎还不能全然满足我们的须要,由于当我们要在本机上开发(phpWeb)或者測试另外 ...

  7. hdu 5338 ZZX and Permutations (贪心+线段树+二分)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  8. 双向队列(STL做法)

    双向队列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 想想双向链表--双向队列的定义差点儿相同,也就是说一个队列的队尾同一 ...

  9. Intel processor brand names-Xeon,Core,Pentium,Celeron----Quark

    http://en.wikipedia.org/wiki/Intel_Quark Intel Quark From Wikipedia, the free encyclopedia     Intel ...

  10. WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView

    Dictionary中的<string, CustomeType>CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是Custome ...