码迷,mamicode.com
首页 > 其他好文 > 详细

poj2443(简单的状态压缩)

时间:2014-10-11 15:40:55      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:poj2443   pku2443   

POJ2443

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2679   Accepted: 1050

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.

Source

POJ Monthly,Minkerui

主要是简单的状态压缩,因为数字最大就是10000,而int最大是32位不到,所以分割成10000/30个,分成40份好了。

之后进行状态压缩,神奇的二进制啊。

经过这种处理后,插入是常数,而判断一个数是否在某一个集合也是常数时间。所以每一个查询判断他是否在集合中也就是O(n)。

典型的空间换时间。

一开始我开了一个1000*10000的数组。memset斗可以导致TLE了。



#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct st{
    int u[400];
    void add(int n){
        int i=n/30,j=n%30;
        u[i]|=(1<<j);
    }
    bool in(int n){
        int i=n/30,j=n%30;
        return u[i]&(1<<j);


    }
    void del(int n){
        int i=n/30,j=n%30;
        u[i]&=~(1<<j);


    }
    void init(){
        memset(u,0,sizeof(u));
              }


};
st v[1001];
int main()
{


    int n,m,i,s,t;
    while(scanf("%d",&n)!=EOF){


            for(i=0;i<n;i++){
                v[i].init();


                scanf("%d",&m);
                while(m--){
                    scanf("%d",&s);
                        v[i].add(s);
                }
            }
            scanf("%d",&m);
            while(m--){
                scanf("%d%d",&s,&t);
                for(i=0;i<n;i++){
                    if(v[i].in(s)&&v[i].in(t)){


                        break;
                    }
                }
                if(i==n){
                    puts("No");
                }else{
                    puts("Yes");
                }
            }


    }
    return 0;
}

poj2443(简单的状态压缩)

标签:poj2443   pku2443   

原文地址:http://blog.csdn.net/dengyaolongacmblog/article/details/39993261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!