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. FTPClient下载文件,程序假死问题

    [所属类包] org.apache.commons.net.ftp.FTPClient [现象描述] 这两天java项目中用到了FTP下载,像之前的项目写好代码,但是点击下载后,程序调试到下面这一行, ...

  2. RCast 66: 射影几何与Rho演算

    Greg Meredith与Isaac DeFrain和Christian Williams一起讨论了射影几何及其在Rho演算中的作用. 原文链接及音频 https://blog.rchain.coo ...

  3. linux下FTP的工具和使用以及rpmReadSignature failed错误

      安装rpm文件时提示rpmReadSignature failed 错误 2011-09-23 11:04 现象: [root@localhost share]# rpm -ivh syslog- ...

  4. MyEclipse 8.6.1 制作绿色版

    我们先在这个目录下新建一个文件: MyEclipse 10.6.bat , 文件内容如下: start eclipse\eclipse.exe -vm jre\bin\javaw.exe 接下来只需要 ...

  5. UDP单播、多播、广播

    一.UDP广播 广播使用的特殊的IP地址:最后一位是255时的IP地址是给广播预留的IP地址,如:192.168.88.255 广播UDP与单播UDP的区别就是IP地址不同,广播使用广播地址255.2 ...

  6. 二十一 JDK注解&注解案例

    什么是注解? 注解和接口,类属于同一个级别 注解可以在变量.方法.类上加载 注解可以有属性也可以没有属性 注解是有作用范围(源码.编译期间,运行期间) 源码期间:例如String类上@Author  ...

  7. python 中常见的异常类型汇总

    异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基类 ...

  8. 面试题之xml解析?

    题目是:用java程序将xml中的数据保存到实体对象中,如何实现? xml如下: <?xml version="1.0" encoding="UTF-8" ...

  9. 连接mysql报错java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized...解决方法

    报错内容: java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents mo ...

  10. JavaScript的调用

    1 方法调用模式 var myObject = { value : 0, increment : function(inc) { alert('hi'); } }; myObject.incremen ...