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】可滚动组件之滚动控制和监听

    前言 可以用ScrollController来控制可滚动组件的滚动位置. 接口描述 ScrollController({ // 初始滚动位置 double initialScrollOffset = ...

  2. 【Flutter】可滚动组件之CustomScrollView

    前言 CustomScrollView是可以使用Sliver来自定义滚动模型(效果)的组件.它可以包含多种滚动模型,举个例子,假设有一个页面,顶部需要一个GridView,底部需要一个ListView ...

  3. (二)数据源处理2-xlrd操作excel

    import xlrd3workbook = xlrd3.open_workbook('test_data.xlsx')sheet =workbook.sheet_by_name('Sheet1')p ...

  4. SQLI-LABS复现通关

    sql-lab 复现通关(深入学习) less-1 基于错误的单引号字符串 - 正常访问 127.0.0.1/?id=1 - 添加 ' 返回报错信息:You have an error in your ...

  5. 【Oracle】 并行查询

    所谓并行执行,是指能够将一个大型串行任务(任何DML,一般的DDL)物理的划分为叫多个小的部分,这些较小的部分可以同时得到处理.何时使用并行执行:1.必须有一个非常大的任务 2.必须有充足的资源(CP ...

  6. Linux下安装配置rocketmq (单个Master、双Master)

    一.环境: centos7(2台虚拟机):192.168.64.123:192.168.64.125 apache-maven-3.2.5(官网要求maven版本是3.2.x,版本不同,编译rocke ...

  7. eNSP启动设备AR1失败记一次解决步骤

    eNSP稳定版本下载:   微信搜索公众号"疯刘小三" 关注后回复ensp即可获得下载链接地址 eNSP V100R002C00B510 Setup.exe 最近在用eNSp的时候 ...

  8. ChannelNets: 省力又讨好的channel-wise卷积,在channel维度进行卷积滑动 | NeurIPS 2018

    Channel-wise卷积在channel维度上进行滑动,巧妙地解决卷积操作中输入输出的复杂全连接特性,但又不会像分组卷积那样死板,是个很不错的想法   来源:晓飞的算法工程笔记 公众号 论文: C ...

  9. http://golang.org/s/better-linker

    http://golang.org/s/better-linker The original linker was also simpler than it is now and its implem ...

  10. REST 架构的替代方案 为什么说GraphQL是API的未来?

    Managing enterprise accounts - GitHub Docs https://docs.github.com/en/graphql/guides/managing-enterp ...