POJ 2443 Set Operation (按位压缩)

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(1<=N<=1000)个集合,每个集合里面有不超过10000个可以重复的元素
给你不超过10000次的询问,每个询问给你两个数,问有没有一个集合同时包含这两个数
思路:按照位进行压缩,因为对与一个数是否在集合中只有在或者不在两种情况于是用一个二进制位存就够了
这样大大节省了空间,一个int是32位,询问的数字最大是10000.我们按照30个一组的分块,一共大概要分400块
这很类似与硬盘的分块...然后我们用位运算维护更新下每一个集合,对于每个询问我们去枚举每一个集合就可以了
 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 using namespace std;
 8 const int BufferSize=1<<16;
 9 char buffer[BufferSize],*head,*tail;
10 inline char Getchar() {
11     if(head==tail) {
12         int l=fread(buffer,1,BufferSize,stdin);
13         tail=(head=buffer)+l;
14     }
15     return *head++;
16 }
17 inline int read() {
18     int x=0,f=1;char c=Getchar();
19     for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
20     for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
21     return x*f;
22 }
23 const int maxn = 1100;
24 const int Size = 30;
25 int n,m,q;
26 struct Set
27 {
28     int st[400];
29     void init (){for (int i=0;i<400;++i) st[i]=0;}
30     int getBlk(int num){return num/Size;}
31     int getIdx(int num){return num%Size;}
32     void Add (int x){st[getBlk(x)]|=(1<<getIdx(x));}
33     void Del (int x){st[getBlk(x)]&=~(1<<getIdx(x));}
34     bool In (int x){return st[getBlk(x)]&(1<<getIdx(x));}
35 };
36 Set a[maxn];
37 int main()
38 {
39     //freopen("de.txt","r",stdin);
40     while (~scanf("%d",&n)){
41         for (int i=0;i<maxn;++i)
42             a[i].init();
43         for (int i=0;i<n;++i){
44             m=read();
45             for (int j=0;j<m;++j){
46                 int x;
47                 x=read();
48                 a[i].Add(x);
49             }
50         }
51         q=read();
52         for (int i=0;i<q;++i){
53             int x,y;
54             x=read();y=read();
55             bool f=false;
56             for (int i=0;i<n;++i){
57                 if (a[i].In(x)&&a[i].In(y)){
58                     f=true;break;
59                 }
60             }
61             if(f) printf("Yes\n");
62             else printf("No\n");
63         }
64     }
65     return 0;
66 }

 




转载于:https://www.cnblogs.com/agenthtb/p/7410554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值