转角的守望

思多久,方为远见!!!

poj 2443 状态压缩 叉姐的魔法训练

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3162   Accepted: 1284

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


题意:

1000个集合,每个集合最多有10000个数字,查询a b 是否同时在某一个集合中

题解:

简单状态压缩,二进制压缩

int a[40][10010]       每一个列1000个数压缩成32个整数,这32个整数里面每个数字的每一个位表示一行

x=i/32,y=i%32;
//x表示集合i在整数x里面的二进制位里面,y表示在集合i在整数x里面的1<<y位中
a[x][temp]|=(1<<y)

这个运算表示把这一行的某一位变为1(即:存在temp这个元素)

if(a[i][num_1]&a[i][num_2])
判断在这个集合中是否同时存在,用位运算计算,直接计算到其中任意一行去了




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int a[40][10010];

int main()
{
    int x,y;
    int n,m,t,temp;
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        x=i/32,y=i%32;
        for(int j=0;j<t;j++){
            scanf("%d",&temp);
            a[x][temp]|=(1<<y);
        }
    }

    n=n/32+1;
    scanf("%d",&m);
    int num_1,num_2;
    while(m--)
    {
        int flag=1;
        scanf("%d%d",&num_1,&num_2);
        for(int i=0;i<n;i++){
            if(a[i][num_1]&a[i][num_2]){
                flag=0;
                puts("Yes");
                break;
            }
        }

        if(flag)
            puts("No");
    }

    return 0;
}



            
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Summer__show_/article/details/53983763
个人分类: 叉姐的魔法训练
想对作者说点什么? 我来说一句

数据结构讲义-叉姐(交大郭晓旭)

2015年08月12日 450KB 下载

没有更多推荐了,返回首页

不良信息举报

poj 2443 状态压缩 叉姐的魔法训练

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭