关闭

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

179人阅读 评论(0) 收藏 举报
分类:

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;
}



   
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

叉姐的魔法训练(第二课)---- 冰系魔法入门

挖坑 ----------------------------- ----------------------------- ----------------------------- -----...
  • cyendra
  • cyendra
  • 2013-10-01 21:27
  • 1849

状态压缩dp入门 (poj3254 Corn Fields)

题目链接:http://poj.org/problem?id=3254 题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多...
  • y990041769
  • y990041769
  • 2014-04-28 19:10
  • 19046

poj2411 2663 2420 dp+状态压缩(多米诺骨牌问题)

题目描述:用1*2 的矩形通过组合拼成大矩形,求拼成指定的大矩形有几种拼法。 首先 我们先求用1*2 的矩形拼成 n*m的矩形有多少种拼法 当n*m为奇数时,一定是不会拼出来的,因为想要拼出来就需...
  • shiwei408
  • shiwei408
  • 2013-04-19 00:04
  • 7295

POJ1184-Smart typist【搜索与状态压缩】

  • 2011-07-31 23:30
  • 10KB
  • 下载

poj2411状态压缩dp训练2——A

图论训练2---A DescriptionSquares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One ...
  • yuanjunlai141
  • yuanjunlai141
  • 2016-03-24 10:36
  • 203

poj 3685 二分套二分 叉姐的魔法训练

Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6251   Accepted: 1791 ...
  • Summer__show_
  • Summer__show_
  • 2016-08-17 21:16
  • 216

poj 3213 叉姐的魔法训练

PM3 Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 3400   Accepted:...
  • Summer__show_
  • Summer__show_
  • 2017-04-18 20:25
  • 234

poj 3244 公式推导 叉姐的魔法训练

Difference between Triplets Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2723 ...
  • Summer__show_
  • Summer__show_
  • 2017-04-17 21:01
  • 372

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

Route Statistics Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Other...
  • firenet1
  • firenet1
  • 2015-08-06 09:23
  • 997

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

#include #include #include #include using namespace std; #define maxn 1<<15 #define ll int #define m...
  • firenet1
  • firenet1
  • 2014-08-04 16:02
  • 677
    个人资料
    • 访问:95666次
    • 积分:4088
    • 等级:
    • 排名:第8777名
    • 原创:388篇
    • 转载:21篇
    • 译文:0篇
    • 评论:15条