POJ 2443 Set Operation

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3558   Accepted: 1479

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
 
 
题意 
有n个集合,给出两个数,判断这两个数在不在同一个集合中。
 
分析 
使用bitset来进行交集操作。b.any()为b中是否存在置为1的二进制位?注意,两个bitset求交集后返回的还是一个bitset。
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 1e4+5;
const int mod = 77200211+233;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
//每个bitset的大小为1024
bitset<1024> b[maxn];
int main(){
    int n,x,y,q;
    while(~scanf("%d",&n)){
        for(int i=0;i<maxn;i++) b[i].reset();

        for(int i=0;i<n;i++){
            scanf("%d",&q);
            while(q--){
                scanf("%d",&x);
                b[x][i]=1;
            }

        }
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&x,&y);

            if((b[x]&b[y]).any()){
                puts("Yes");
            }else {
                puts("No");
            }

        }
    }
    return 0;
}

 

 
 
posted @ 2018-03-25 09:35  litos  阅读(162)  评论(0编辑  收藏  举报