zoukankan      html  css  js  c++  java
  • POJ2443 Set Operation (基础bitset应用,求交集)

    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.

    题意:给定N个集合,Q次询问,对每次询问,求X,Y是否在同一集合出现过,注意X=Y时,X在一个集合里至少出现一次就满足了。

    思路:用Bitset来表示每个元素在哪些集合出现过,如果X和Y出现的集合有交集,则满足。

    积累一下:

              s.set()是全部置为1,s.set(pos)是某一位置为1,s.reset()是全部置为0.

              s.flip(),按位取反。

    #include<cstdio>
    #include<bitset>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=10000;
    bitset<1010>s[maxn+10];
    int read()
    {
        char c=getchar(); int res;
        while(c>'9'||c<'0') c=getchar();
        for(res=0;c>='0'&&c<='9';c=getchar()) res=(res<<3)+(res<<1)+c-'0';
        return res;
    }
    int main()
    {
        int N,Q,i,num,x,y;
        while(~scanf("%d",&N)){
            for(i=1;i<=maxn;i++) s[i].reset();
            for(i=1;i<=N;i++){
                num=read();
                while(num--){
                    x=read();
                    s[x][i]=1; //s[x].set(i);
                }
            }
            Q=read();
            while(Q--){
                x=read(); y=read();
                if((s[x]&s[y]).count()) puts("Yes"); //.any()
                else puts("No");
            }
        }
        return 0;
    }
  • 相关阅读:
    二:dot语言语法及使用
    一:安装graphviz
    一个程序的前世今生(四)——延迟绑定和GOT与PLT
    一个程序的前世今生(三)——动态链接库和静态链接库
    一个程序的前世今生(二)——可执行文件如何加载进内存
    更新mysql驱动5.1-47 Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEY
    The superclass javax servlet http HttpServlet was not found on the Java Build Path
    vue-router地址栏URL全局参数拼接
    Canvas签字画图板
    Vue 表单拖拽排序
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8537405.html
Copyright © 2011-2022 走看看