zoukankan      html  css  js  c++  java
  • POJ2443 Set Operation —— bitset

    题目链接:https://vjudge.net/problem/POJ-2443

    Set Operation
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 3554   Accepted: 1477

    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个集合,每个集合有若干个数。有m个询问,问x、y是否存在于同一个集合中。

    题解:

    C++ bitset的应用。

    具体介绍:https://blog.csdn.net/qll125596718/article/details/6901935

    成员函数函数功能
    bs.any() 是否存在值为1的二进制位
    bs.none() 是否不存在值为1的二进制位
    或者说是否全部位为0
    bs.size() 位长,也即是非模板参数值
    bs.count() 值为1的个数
    bs.test(pos) 测试pos处的二进制位是否为1
    与0做或运算
    bs.set() 全部位置1
    bs.set(pos) pos位处的二进制位置1
    与1做或运算
    bs.reset() 全部位置0
    bs.reset(pos) pos位处的二进制位置0
    与0做或运算
    bs.flip() 全部位逐位取反
    bs.flip(pos) pos处的二进制位取反
    bs.to_ulong() 将二进制转换为unsigned long输出
    bs.to_string() 将二进制转换为字符串输出
    ~bs 按位取反
    效果等效为bs.flip()
    os << b 将二进制位输出到os流
    小值在右,大值在左

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #include <bitset>   //bitset头文件
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 1e5+10;
    19 
    20 bitset<1010>a[10010];
    21 int main()
    22 {
    23     int n;
    24     while(scanf("%d", &n)!=EOF)
    25     {
    26         for(int i = 1; i<1010; i++)
    27             a[i].reset();
    28         for(int i = 1; i<=n; i++)
    29         {
    30             int m, x;
    31             scanf("%d", &m);
    32             while(m--)
    33             {
    34                 scanf("%d", &x);
    35                 a[x][i] = 1;
    36             }
    37         }
    38 
    39         int m, x, y;
    40         scanf("%d", &m);
    41         while(m--)
    42         {
    43             scanf("%d%d", &x,&y);
    44             if((a[x]&a[y]).count()) puts("Yes");
    45             else puts("No");
    46         }
    47     }
    48 }
    View Code
  • 相关阅读:
    drf的模型基表与10个单群操作方法
    drf的序列化组件
    drf之请求模块,渲染模块,解析模块,响应模块,异常模块
    drf之接口与接口规范
    MongoDB C# Demo
    如何用Dome4j(2.2.1)创建Xml
    Map、Set的基本概括
    如何自定义xml文件
    HashMap和HashTable的理解与区别
    装箱 拆箱
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8641792.html
Copyright © 2011-2022 走看看