poj 2443 状态压缩 叉姐的魔法训练

原创 2017年01月02日 23:10:20

Set Operation
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3162   Accepted: 1284

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


题意:

1000个集合,每个集合最多有10000个数字,查询a b 是否同时在某一个集合中

题解:

简单状态压缩,二进制压缩

int a[40][10010]       每一个列1000个数压缩成32个整数,这32个整数里面每个数字的每一个位表示一行

x=i/32,y=i%32;
//x表示集合i在整数x里面的二进制位里面,y表示在集合i在整数x里面的1<<y位中
a[x][temp]|=(1<<y)

这个运算表示把这一行的某一位变为1(即:存在temp这个元素)

if(a[i][num_1]&a[i][num_2])
判断在这个集合中是否同时存在,用位运算计算,直接计算到其中任意一行去了




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int a[40][10010];

int main()
{
    int x,y;
    int n,m,t,temp;
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        x=i/32,y=i%32;
        for(int j=0;j<t;j++){
            scanf("%d",&temp);
            a[x][temp]|=(1<<y);
        }
    }

    n=n/32+1;
    scanf("%d",&m);
    int num_1,num_2;
    while(m--)
    {
        int flag=1;
        scanf("%d%d",&num_1,&num_2);
        for(int i=0;i<n;i++){
            if(a[i][num_1]&a[i][num_2]){
                flag=0;
                puts("Yes");
                break;
            }
        }

        if(flag)
            puts("No");
    }

    return 0;
}




            
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu 5330 Route Statistics 2015多校联合训练赛4 状态压缩

Route Statistics Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Other...

hdu 4899 Hero meet devil 2014多校联合训练赛 状态压缩dp 最长公共子序列

#include #include #include #include using namespace std; #define maxn 1<<15 #define ll int #define m...

POJ 1185 炮兵阵地 经典状态压缩DP

先说下我个人的理解啊! 因为炮的攻击范围是上下左右两个格子  所以dp数组里面要保证存有第i行的状态和第i-1行的状态,一个三维数组搞定,同样判断的时候也要判断两行!其他的跟poj3254没什么区别!...

poj2777线段树,状态压缩

题意: 就是一堆石头,开始全是一种颜色; 之后叫你染色,颜色不超过30种; 染色的规则是一段一段染; 叫你求某一段区间的颜色种数; 理解: 这题一看就是线段树; 主要就是记录...

POJ3311——Hie with the Pie(floyd,状态压缩dp,旅行商)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6506 ...

POJ 3411-Paid Roads(状态压缩+dijkstra算法+floyd-warshall算法)

题目大意:给出一张有向图,求点1到点N的最短路,不同的是,对于每一条边,除了源点目标点和花费以外,还有额外点c,若走这条边之前到达过c点,花费会减少到另一个值P。如果最短路不存在,输出impossib...

poj3254——Corn Fields——————【状态压缩】

/**     解题思路:首先将原图每一行当做一个二进制数,按位取反得到另一个十进制数。然后求出在所给的列的范围内的合法状态,即没有任意两个1相邻,放入数组legal中,每种合法状态对应数组的一个下...

POJ 1170 Shopping Offers 状态压缩dp+完全背包

题意: 有n种物品需要你去购买,每种物品购买num【i】个。(1 ) 当然提供s种优惠:购买商家规定的组合商品的价格为 s【i】 2 7 3 2 8 2 5 2 1 7 3 5 2 7 1 ...

POJ 3311 Hie with the Pie (Floyd + 暴力全排列 || Floyd + 状态压缩DP (已补充))

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6782   Accepted:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)