Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2965   Accepted: 1196

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.

题意是给出了N个的集合,一个集合中有若干个数。然后是Q个询问,两个数是否在一个集合中。

发现一共最多就1000个集合,假设这个集合标号为N,那么能发现 h=N/32;k=N%32; h与k就可以对这个集合标号N进行标识了。所以用 a[h][j]= k 表示j这个数在 h*32 + (k二进制位为1的那个标识位) 这个集合中。

然后询问两个数是否在一个集合中,只需询问在h相同的情况下,两个k1,k2相与是否大于0,大于0说明存在两个都是1的位置,就说明这两个数曾在一个集合中了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int a[32][10001];
int n,q; int main()
{
int i,j,f,h,k,num;
bool flag;
memset(a,0,sizeof(0));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&f);
h=i%32;
k=i/32;
for(j=1;j<=f;j++)
{
scanf("%d",&num);
a[h][num] = a[h][num]|(1<<k);
}
}
scanf("%d",&q);
for(i=1;i<=q;i++)
{
scanf("%d%d",&h,&k);
flag=false;
for(j=0;j<32;j++)
{
if(a[j][h]&a[j][k])
{
flag=true;
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2443:Set Operation 经典位运算好题的更多相关文章

  1. POJ 2443 Set Operation (按位压缩)

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

  2. POJ 2443 Set Operation(压位加速)

    http://poj.org/problem?id=2443 题意: 有1000个集合,每个集合有至多10000个数,之后输入多个询问,判断询问的两个数是否位于同一个集合. 思路: 位运算...很强大 ...

  3. [poj]开关类问题 枚举 位运算

    poj 1222  EXTENDED LIGHTS OUT 开关只有两种方案 按和不按,按两次相当于关 只用枚举第一排开关的按法即可,剩下的行为使上一排的灯全部关闭,按法可以确定,并且是唯一的. 最后 ...

  4. POJ 1166 The Clocks [BFS] [位运算]

    1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...

  5. 蓝桥杯---汉字取首字母(位运算 & 水题)

    确实题目虽然有点水,但是开始的时候好像还真的没有想到怎么提取出这个编号一不小心感觉可以可以用unsigned char 这种类型,直接转为16进制,但是之后发现虽然第一次在codeblock中还行,但 ...

  6. 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)

    点此看题面 大致题意: 给你\(n\)个\(m\)位二进制数.每组询问给你一个\(m\)位二进制数,要求你从\(0\)开始,依次对于这\(n\)个数进行\(and\)或\(or\)操作,问有多少种方案 ...

  7. 【BZOJ4300】绝世好题(位运算水题)

    点此看题面 大致题意: 给你一个序列\(a\),让你求出最长的一个子序列\(b\)满足\(b_i\&b_{i-1}!=0\). 位运算+\(DP\) 考虑设\(f_i\)表示以第\(i\)个数 ...

  8. zoj--3870--Team Formation(位运算好题)

    Team Formation Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  9. zzulioj--1832--贪吃的松鼠(位运算好题)

    1832: 贪吃的松鼠 Time Limit: 3 Sec  Memory Limit: 2 MB Submit: 43  Solved: 7 SubmitStatusWeb Board Descri ...

随机推荐

  1. Linux CentOS7 VMware find命令、文件名后缀

    一.find命令 Linux系统中的 find 命令在查找文件时非常有用而且方便.它可以根据不同的条件来查找文件,例如权限.拥有者.修改日期/时间.文件大小等等.在这篇文章中,我们将学习如何使用 fi ...

  2. JDBC--Statement使用

    1.通过Statement实现类执行更新操作(INSERT.UPDATE .DELETE): --1)获取数据库连接Connection的对象: --2)通过Connection类的createSta ...

  3. WinCC V7.5安装过程截图

  4. Docker registry自签名证书

    权威Registry 获取安全证书有两个办法:互联网认证的CA处获取.自建CA自己给自己签名. 1.从认证CA处获取签名证书,大多数是需要付出一定费用的,近些年也有认证CA提供免费证书,例如Let’s ...

  5. Jquery事件汇总、网页入口

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Python中的*可变参数与**关键字参数

    1.定义了一个需要两个参数的函数 def print_str(first, second): print first print second if __name__ == "__main_ ...

  7. 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)

    #1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...

  8. centos6 初次安装成功,未显示eth0网卡的信息

    https://www.cnblogs.com/yecao8888/p/6364830.html

  9. java 移动距离

    移动距离 X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3- 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下: 1 2 3 4 5 ...

  10. python集成开发环境Anaconda的安装

    参考博文: anaconda在Linux下的安装 Linux下anaconda3的安装 Anaconda的安装.启用及停用的步骤 Python学习之Anaconda的使用及配置方法 Anaconda ...