题目链接:https://vjudge.net/problem/POJ-2443

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3554   Accepted: 1477

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个集合,每个集合有若干个数。有m个询问,问x、y是否存在于同一个集合中。

题解:

C++ bitset的应用。

具体介绍:https://blog.csdn.net/qll125596718/article/details/6901935

成员函数 函数功能
bs.any() 是否存在值为1的二进制位
bs.none() 是否不存在值为1的二进制位
或者说是否全部位为0
bs.size() 位长,也即是非模板参数值
bs.count() 值为1的个数
bs.test(pos) 测试pos处的二进制位是否为1
与0做或运算
bs.set() 全部位置1
bs.set(pos) pos位处的二进制位置1
与1做或运算
bs.reset() 全部位置0
bs.reset(pos) pos位处的二进制位置0
与0做或运算
bs.flip() 全部位逐位取反
bs.flip(pos) pos处的二进制位取反
bs.to_ulong() 将二进制转换为unsigned long输出
bs.to_string() 将二进制转换为字符串输出
~bs 按位取反
效果等效为bs.flip()
os << b 将二进制位输出到os流
小值在右,大值在左

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#include <bitset> //bitset头文件
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; bitset<>a[];
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<; i++)
a[i].reset();
for(int i = ; i<=n; i++)
{
int m, x;
scanf("%d", &m);
while(m--)
{
scanf("%d", &x);
a[x][i] = ;
}
} int m, x, y;
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &x,&y);
if((a[x]&a[y]).count()) puts("Yes");
else puts("No");
}
}
}

POJ2443 Set Operation —— bitset的更多相关文章

  1. 【bitset】poj2443 Set Operation

    模板题.S[i][j]表示i是否存在于第j个集合里.妈蛋poj差点打成poi(波兰无关)是不是没救了. #include<cstdio> #include<bitset> us ...

  2. POJ2443 Set Operation (基础bitset应用,求交集)

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

  3. [POJ2443]Set Operation(bitset)

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

  4. [POJ 2443] Set Operation (bitset)

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

  5. poj2443Set Operation (bitset)

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

  6. poj2443(简单的状态压缩)

    POJ2443 Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2679   Accepted:  ...

  7. Bitset小结 (POJ2443 & HDU4920)

    学了下bitset用法,从网上找的一些bitset用法,并从中调出一些常用的用法. 构造函数bitset<n> b; b有n位,每位都为0.参数n可以为一个表达式.如bitset<5 ...

  8. POJ244Set Operation(bitset用法)

    Bryce1010模板 /* 题意:给出n个集合(n<=1000),每个集合中最多有10000个数, 每个数的范围为1~10000,给出q次询问(q<=200000), 每次给出两个数u, ...

  9. 【Bitset】重识

    ---------------------------------------------------------------------------- 一题题目: 一题题解: 这个题目哪来入门再好不 ...

随机推荐

  1. checkbox 自动换行

    把匹配的checkbox和文字用一对span标签包裹 并且给这个span标签加样式 display:inline-block <span style="display:inline-b ...

  2. k8s学习(二)——etcdctl工具的使用

    k8s的实现核心实际上就是通过读写etcd数据库实现对资源的存储,管理和控制. k8s所有资源的本源都是存储在etcd中的一个个键值对. 理论上可以观察到etcd数据库中的数据变化.具体的使用方式如下 ...

  3. 转:代码管理技巧——两步创建本地SVN服务器图文教程

    from: http://www.cnblogs.com/tianhonghui/archive/2012/07/22/2603454.html   当我们进行开发的时候,不论是独立开发还是处在团队中 ...

  4. DevExpress控件之TreeList

    基于v18.1 使用AppendNode方法手动赋值时,首先要添加treeListColumn 默认样式                                     修改后的样式   1 ...

  5. Archlinux休眠设置

    2017-03-11 更新: 优化部分文字描述; 默认情况下禁用 swap 分区, 当执行休眠操作时先启用 swap 分区, 然后再执行休眠操作(给 /usr/bin/{swapon,swapoff} ...

  6. 【Python】创建和使用类

    面向对象编程是最有效的软件编写方法之一 创建Dog类 class Dog(): '''一次模拟小狗的简单测试''' def __init__(self,name,age): self.name = n ...

  7. const的理解、const指针、指向const的指针

    1.const 的理解 const 是C语言的一个关键字,需要注意的是,const 关键字是把变量变为一个只读的变量(也就是不可以作为左值),绝对不是将这个变量变为常量.也就是说经过const 修饰的 ...

  8. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

  9. android开发系列之使用xml自定义控件

    在android开发的过程中,有的时候面对多个Activity里面一些相同的布局,我们需要写多次相同的代码,同时这种方法给我们的项目维护也带来了很大不便.那么有没有一种可行的办法能够将Activity ...

  10. apt-mirror 校验错误文件处理

    apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...