题目链接: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. GIS可视化

    作为一名GIS专业的学生,一晃也毕业三年了,在supermap也呆了三年多了,做的最多的就是浏览器端的GIS展示,最近也想分享一下我们团队在浏览器端GIS可视化的一些成果,算是做个宣传吧!有用的着的可 ...

  2. VC++动态链接库(DLL)编程深入浅出(四)

    这是<VC++动态链接库(DLL)编程深入浅出>的第四部分,阅读本文前,请先阅读前三部分:(一).(二).(三). MFC扩展DLL的内涵为MFC的扩展,用户使用MFC扩展DLL就像使用M ...

  3. 【Redmine】Redmine 3.0.1 安装与配置

    Redmine安装 VM安装Linux 安装Bitnami Redmine 配置环境 1.VM安装Linux 使用虚拟机安装Linux 本文使用的是Centos(CentOS-6.3-x86_64-b ...

  4. 4pda.ru注冊验证的解码算法

    代码源于看雪林版在我群里介绍注冊一个俄文安卓论坛.发出来了链接大家在測试注冊. http://4pda.ru/forum/index.php? 註册方式請参看: _https://forum.tuts ...

  5. JS创建表单提交备份

    //保存 function saveFT() { var data = { createDate: GetDateStr(0), name: $("#txtName").val() ...

  6. ubuntu在terminal下安装mysql

    安装的时候.仅仅须要在terminal中输入下面几条命令 1.sudo apt-get install mysql-server 2.apt-get isntall mysql-client 3. s ...

  7. C++ Singleton + MultiThread

    #include <iostream> #include <mutex> using namespace std; template <class T> class ...

  8. msgsnd的一个小问题

    今天写了一个System V消息队列的小样例.定义了一个例如以下的结构体: #define MSG_SIZE 8192 struct request { long mtype; int client_ ...

  9. Android之Intent和Activity

    Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...

  10. ubantu 彻底卸载mysql

    卸载mysql 第一步 1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server 3 ...