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

题意:有n个集合,每个集合里有c[i]个数,可能重复,共有m个操作,每个操作询问两个数,问这两个数是否在n个集合中的某一个同时出现。

思路:一开始标记每一个集合中出现的数,然后O(n*m)的复杂度T了,换了bitset的思路,即用bitset<1005>bt[10005]记录第i个元素在第j个集合出现的情况,然后对于任意两个数a,b,只要用(bt[a]&bt[b]).any()判断一下是否出现过就行。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#include<bitset>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
bitset<1005>bt[10005]; int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=10000;i++)bt[i].reset();
for(i=1;i<=n;i++){
scanf("%d",&c);
for(j=1;j<=c;j++){
scanf("%d",&d);
bt[d][i]=1;
}
}
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
if((bt[c]&bt[d]).any() )printf("Yes\n");
else printf("No\n"); }
}
return 0;
}

poj2443Set Operation (bitset)的更多相关文章

  1. POJ2443 Set Operation —— bitset

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

  2. [POJ 2443] Set Operation (bitset)

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

  3. 压位加速-poj-2443-Set Operation

    题目链接: http://poj.org/problem?id=2443 题目意思: 有n个集合(n<=1000),每个集合有m个数ai(m<=10000,1=<ai<=100 ...

  4. 【bitset】poj2443 Set Operation

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

  5. 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 ...

  6. POJ244Set Operation(bitset用法)

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

  7. [POJ2443]Set Operation(bitset)

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

  8. a bitwise operation 广告投放监控

    将随着时间不断增大的数字N个依次编号为1到N的N个球,颜色每次随机为红黑蓝,时间上先后逐个放入篮子中,计算离现在最近的24个球的红.黑.蓝颜色数 广告投放监控 a bitwise operation ...

  9. bitset常用函数用法记录 (转载)

    有些程序要处理二进制位的有序集,每个位可能包含的是0(关)或1(开)的值.位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法.标准库提供了bitset类使得处理位集合更容易一些.要使 ...

随机推荐

  1. Flutter 基础组件:输入框和表单

    前言 Material组件库中提供了输入框组件TextField和表单组件Form. 输入框TextField 接口描述 const TextField({ Key key, // 编辑框的控制器,通 ...

  2. Fail2ban工具使用

    Fail2ban ​ fail2ban扫描日志文件并且可以识别禁用某些多次尝试登录的IP,通过更新系统的防火墙规则来实现拒绝该IP连接,也可以配置禁用的时间.fail2ban提供了一些常用软件默认的日 ...

  3. Python基础语法5-控制流语句

  4. 【Java】标识符

    一.标识符 文章目录 一.标识符 1.标识符的命名规则 2.关键字.保留字.特殊值 3.code Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符.简单的说,凡是程序员自己命名的部分 ...

  5. 【Problems】端口被占用 查看是被谁占用并关闭它

    文章目录 Windows Linux 经常在Windows.Linux环境下运行JavaWeb项目,Tomcat的端口被占用了. 端口被占用就查看是被谁占用关闭它就行. Windows 在Window ...

  6. awk -v参数

    -v var=val --assign var=val Assign the value val to the variable var, before execution of the progra ...

  7. 使用.net中的API网关模式封装微服务

    在本文中,我们将了解如何使用API网关模式来封装微服务并抽象出底层实现细节,从而允许使用者拥有进入我们系统的一致入口点. 为了构建和测试我们的应用程序,我们需要: 1.Visual Studio 20 ...

  8. dubbo快速入门demo

    参考文章 https://blog.csdn.net/abcwanglinyong/article/details/81906027 该demo包含三个项目,分别是: 服务提供端项目:provider ...

  9. 安装git-macOS系统

    通过homebrew安装Git 1.安装homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  10. 阿里云VOD(三)

    一.视频播放器 参考文档:https://help.aliyun.com/document_detail/125570.html?spm=a2c4g.11186623.6.1083.1c53448bl ...