poj 2443

本文介绍了一个集合操作查询问题的算法实现。该问题涉及处理多个可能包含重复元素的集合,并通过高效的位运算方法来判断任意两个元素是否至少在一个共同的集合中出现。文章详细解释了输入输出格式,并给出了一段具体的C++代码示例。
Set Operation
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2961 Accepted: 1192

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.
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int n,m;
int a[32][10005];
int main()
{
    int num,tt,q;
    while(scanf("%d",&n)!=EOF)
    {
        bool flag;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num);
            int xx,yy;
            xx=i/32;
            yy=i%32;
            for(int j=0;j<num;j++)
            {
                scanf("%d",&tt);
                a[xx][tt]|=(1<<yy);
            }
        }
        m=n/32;
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int u,v;
            flag=false;
            scanf("%d%d",&u,&v);
            for(int j=0;j<=m;j++)
            {
                if(a[j][u]&a[j][v])
                {
                    flag=1;
                    break;
                }
            }
            if(flag)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/water-full/p/4815063.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值