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. 【JavaWeb】Cookie&Session

    Cookie&Session Cookie 什么是 Cookie Cookie 即饼干的意思 Cookie 是服务器通知客户端保存键值对的一种技术 客户端有了 Cookie 后,每次请求都发送 ...

  2. (二)数据源处理4-excel部分封装及数据转换

    excel02.py # -*- coding: utf-8 -*-#@File :excel_oper_02.py#@Auth : wwd#@Time : 2020/12/7 8:16 下午impo ...

  3. CVE-2019-15107_webmin漏洞复现

    一.漏洞描述 Webmin的是一个用于管理类Unix的系统的管理配置工具,具有网络页面.在其找回密码页面中,存在一处无需权限的命令注入漏洞,通过这个漏洞攻击者即可以执行任意系统命令.它已知在端口100 ...

  4. mysql的逻辑备份和恢复

    备份指定的数据库或此数据库中的某些表 mysqldump [options] db_name [tables] >backup.sql 备份指定的一个或多个数据库 mysqldump --dat ...

  5. leetcode230. 二叉搜索树中第K小的元素

    题目链接: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 题目: 给定一个二叉搜索树,编写一个函数 kthSmalle ...

  6. web dynpro配置注意事项

    如果你想使用web dynpro 开发的应用,但是发现浏览器报错,那么你按照下面的步骤逐一进行检查吧.特别是返回的500错误,或者是你发现浏览器的地址栏中以http://<hostname> ...

  7. Ice框架介绍

    概述 Ice是一个开源的综合性RPC框架,以高性能和原生支持微服务的架构而著称.提供了很多可以直接使用的组件,如注册中心IceGrid,部署工具IcePatch2,防火墙穿透Glacier2,发布订阅 ...

  8. Docker 建站小记

    一,前言 Docker 建站小记,我使用了四个镜像来搭建:nginx,certbot,mysql,gradle.欢迎访问:https://www.zzk0.top 这个网页是从 github 上找的个 ...

  9. 学习Java第一天

    public 保证类名和文件名一致 关键字字母全小写,编辑器中有颜色标记 null空常量不能打印 变量就是内存中的存储空间 计算机中最小的存储单元时字节(byte) //1字节(B) = 8位(bit ...

  10. SQLHelper ------ python实现

    SQLHelper ------ python实现 1.第一种: import pymysql import threading from DBUtils.PooledDB import Pooled ...