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. 「CF600E」Lomsat gelral

    传送门 Luogu 解题思路 线段树合并板子题(也可以 dsu on the tree) 好像没什么好讲的,就是要注意开 long long . 细节注意事项 咕咕咕 参考代码 #include &l ...

  2. 「POI2015」KIN

    传送门 Luogu 解题思路 想要做这道题,只要会维护区间最大子段和就好了. 而这个可以用线段树维护模板点这里 对于重复的情况,我们可以对每一个位置记一个前驱表示和当前位置种类相同的前一个位置. 然后 ...

  3. JSTL1.0和JSTL1.1的区别

    这要从一个异常说起 According to TLD or attribute directive in tag file, attribute value does not accept any e ...

  4. hyper-V pcie直通

    横河VI702失败. 显卡没有提示失败,但分配给的那个虚拟机中 检测不到独占的显卡,不知道那里出错了. 参考教程22#:[网络] 既然最新hyper V也支持pcie直通.是不是可以扔掉esxi了?

  5. sklearn实现多分类逻辑回归

    sklearn实现多分类逻辑回归 #二分类逻辑回归算法改造适用于多分类问题1.对于逻辑回归算法主要是用回归的算法解决分类的问题,它只能解决二分类的问题,不过经过一定的改造便可以进行多分类问题,主要的改 ...

  6. 十七 Ajax&校验用户名功能

    Ajax: 即"Asynchronous JavaScript And XML", 异步JavaScript和XML , 是指一种创建的交互式页面应用的网页开发技术,它并不是新的技 ...

  7. html css3

    一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内部样式表(在hea ...

  8. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project sharp-common: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin

    [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.sharp:sharp-common > ...

  9. php二位数组排序(按子元素排序)

    array_multisort(array_column($mainTree[$note]["beCalls"], "wtp"), SORT_DESC, arr ...

  10. R 《回归分析与线性统计模型》page120,4.3

    #P120习题4.3 rm(list = ls()) A = read.xlsx("xiti_4.xlsx",sheet = 3) names(A) = c("ord&q ...