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. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

  2. Appium学习笔记4_元素定位方法

    Appium之元素定位,如果对Android上如何使用工具获取页面元素有问题的,请转战到这:http://www.cnblogs.com/taoSir/p/4816382.html. 下面主要是针对自 ...

  3. 关于python requests 包跑ssl的设置 和 charles相关抓包的问题

    由于在测试服务器上测试东西都是https,然后最近又在和大神一起开发openapi,api写好当然是要测试的 python上测试接口最好用的莫过于requests模块了.但是 我还从来没有用reque ...

  4. Kivy 中文教程 实例入门 简易画板 (Simple Paint App):3. 随机颜色及清除按钮

    1. 随机颜色 通过前面的教程,咪博士已经带大家实现了画板的绘图功能.但是,现在画板只能画出黄色的图案,还十分单调,接下来咪博士就教大家,如何使用随机颜色,让画板变得五彩斑斓. 改进后的代码如下: f ...

  5. 在腾讯云&阿里云上部署JavaWeb项目(Tomcat+MySQL)

    之前做项目都是在本地跑,最近遇到需要在在云服务器(阿里云或者腾讯云都可以,差不多)上部署Java Web项目的问题,一路上遇到了好多坑,在成功部署上去之后写一下部署的步骤与过程,一是帮助自己总结记忆, ...

  6. HTMl Dom操作

    訪問: getElementById():帶回指定id的所有元素 getElementByTagName():帶回指定標籤名的所有的元素 getElementByClassName():帶回指定類型名 ...

  7. schema举例一

    schema举例一: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs=& ...

  8. String在内存中如何存储(Java)

    JDK1.8中JVM把String常量池移入了堆中,同时取消了“永久代”,改用元空间代替(Metaspace)java中对String对象特殊对待,所以在heap区域分成了两块,一块是字符串常量池(S ...

  9. easyui 功能介绍

    最近使用easyui, 下面,我介绍下常用功能: //绑定Product总类型 $('#gdv_ProductParentType').datagrid({ url: '../api/BindData ...

  10. vs2017 C4996 错误

    严重性    代码    说明    项目    文件    行    禁止显示状态错误    C4996    'strcpy': This function or variable may be ...