zoukankan      html  css  js  c++  java
  • poj2443Set Operation (bitset)

    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个集合,每个集合里有c[i]个数,可能重复,共有m个操作,每个操作询问两个数,问这两个数是否在n个集合中的某一个同时出现。

    思路:一开始标记每一个集合中出现的数,然后O(n*m)的复杂度T了,换了bitset的思路,即用bitset<1005>bt[10005]记录第i个元素在第j个集合出现的情况,然后对于任意两个数a,b,只要用(bt[a]&bt[b]).any()判断一下是否出现过就行。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    #include<bitset>
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 1005
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    bitset<1005>bt[10005];
    
    int main()
    {
        int n,m,i,j,c,d;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=10000;i++)bt[i].reset();
            for(i=1;i<=n;i++){
                scanf("%d",&c);
                for(j=1;j<=c;j++){
                    scanf("%d",&d);
                    bt[d][i]=1;
                }
            }
            scanf("%d",&m);
            for(i=1;i<=m;i++){
                scanf("%d%d",&c,&d);
                if((bt[c]&bt[d]).any() )printf("Yes
    ");
                else printf("No
    ");
    
            }
        }
        return 0;
    }
    


  • 相关阅读:
    LeetCode Merge Two Sorted Lists 归并排序
    LeetCode Add Binary 两个二进制数相加
    LeetCode Climbing Stairs 爬楼梯
    034 Search for a Range 搜索范围
    033 Search in Rotated Sorted Array 搜索旋转排序数组
    032 Longest Valid Parentheses 最长有效括号
    031 Next Permutation 下一个排列
    030 Substring with Concatenation of All Words 与所有单词相关联的字串
    029 Divide Two Integers 两数相除
    028 Implement strStr() 实现 strStr()
  • 原文地址:https://www.cnblogs.com/herumw/p/9464533.html
Copyright © 2011-2022 走看看