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

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

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.

Source

POJ Monthly,Minkerui
 
 
题意 
有n个集合,给出两个数,判断这两个数在不在同一个集合中。
 
分析 
使用bitset来进行交集操作。b.any()为b中是否存在置为1的二进制位?注意,两个bitset求交集后返回的还是一个bitset。
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 1e4+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
//每个bitset的大小为1024
bitset<> b[maxn];
int main(){
int n,x,y,q;
while(~scanf("%d",&n)){
for(int i=;i<maxn;i++) b[i].reset(); for(int i=;i<n;i++){
scanf("%d",&q);
while(q--){
scanf("%d",&x);
b[x][i]=;
} }
scanf("%d",&q);
while(q--){
scanf("%d%d",&x,&y); if((b[x]&b[y]).any()){
puts("Yes");
}else {
puts("No");
} }
}
return ;
}
 
 

POJ 2443 Set Operation的更多相关文章

  1. [POJ 2443] Set Operation (bitset)

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

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

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

  3. POJ 2443 Set Operation (按位压缩)

    Description You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set ...

  4. POJ 2443 Set Operation 题解

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

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

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

  6. bitset常用用法&&简单题分析

    Preface bitset,还是一个比较好用的STL,可以给一些题目做到神奇的常数优化(\(O(\frac{原来的复杂度}{机器的位数(32位or64位)})\)) 关于一些具体的函数等内容可以参考 ...

  7. bitset骚操作

    bitset的用途挺多的,是一个比较骚的常数优化 一.很多位数的二进制数 poj 2443 http://poj.org/problem?id=2443 直接开个1万位的二进制数,求交就行了. 有关集 ...

  8. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  9. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

随机推荐

  1. java mail session使用Properties的clone方法

    /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreem ...

  2. SMBv1 is not installed by default in Windows 10 Fall Creators Update 2017 and Windows Server, Semi-annual Channel

    windows 10 rs3 release enable SMBv1 windows 10 rs3 release file sharing https://support.microsoft.co ...

  3. 彻底弄懂jsonp原理及实现方法

    一. 同源策略 所有支持Javascript的浏览器都会使用同源策略这个安全策略.看看百度的解释: 同源策略,它是由Netscape提出的一个著名的安全策略. 现在所有支持JavaScript 的浏览 ...

  4. 美化centos7

    在美化前,我们先安装一个扩展源.yum install -y epel-release然后安装字体包yum -y install liberation-mono-fonts 安装gnome-menis ...

  5. 反编译微信小程序

    最近看了个微信小程序古诗词全集,想知道他的前后端是怎么实现的,所以就想到了反编译.小程序安装后会有个wxapkg格式的文件存在/data/data/com.tencent.mm/MicroMsg/** ...

  6. codeforces146A

    Lucky Ticket CodeForces - 146A Petya loves lucky numbers very much. Everybody knows that lucky numbe ...

  7. windows部分常用命令

    dir 查看内容 md 新建目录 copy 复制 del 删文件 cls 清屏 tasklist 查看运行进程 taskkill /pid xxx 杀死进程xxx taskmgr 打开任务管理器 ms ...

  8. MT【8】和e有关的一个极限

    解答: 评:这里涉及到e有关的极限的单调性,求导数得:

  9. 洛谷 P2149 [SDOI2009]Elaxia的路线 解题报告

    P2149 [SDOI2009]Elaxia的路线 题目描述 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia ...

  10. bzoj4985 评分 (二分答案+dp)

    首先二分一个答案x,然后我们把>=x的数看成1,<x的数看成0,那如果最后剩下1,这个答案就是合法的. 那我们就来算让某一位得1至少需要填几个1(设这个值是f[i]) i=1..n时,显然 ...