ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Set Operation / OpenJ_Bailian - 2443

2020-03-18 19:04:41  阅读:262  来源: 互联网

标签:2443 Set No there 整数 set bitset Yes Operation


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表示N个集合
接下来N行每行先输入一个整数C表示某个集合的数字个数
然后输入C个整数表示这个集合的所有元素
接下来输入一个整数q
然后是q行
每行两个整数x y
判断x 与 y是否可以属于同一个集合
如果可以输出Yes否则输出No
N <= 1000, C <= 10000 , x, y <= 10000, Q <= 200000

题解

用 bitset
C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间。
本体用到: .set()    .test()
~~具体可看到其它博客~~

代码

#include<bits/stdc++.h>
using namespace std;

int n, m, x, y;
bitset<3010> bit[10010];

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        for(int j = 1; j <= x; j++){
            scanf("%d", &y);
            bit[y].set(i);//y下标中, 第i个位置的下标设为1, 表示y在第i组中
        }
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &x, &y);
        for(int j = 1; j <= n; j++){//每个数组遍历一遍, 找有没有符合条件的数组
            if(bit[x].test(j) && bit[y].test(j)){//x, y的下标j是否都为1, 表示它们是否都在同一组内
                printf("Yes\n");
                x = -1;//标记一下
                break;
            }
        }
        if(x != -1)
            printf("No\n");
    }
    return 0;
}

标签:2443,Set,No,there,整数,set,bitset,Yes,Operation
来源: https://www.cnblogs.com/Little-Turtle--QJY/p/12519367.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有