HDU 3006 The Number of set (状态压缩)

The Number of set

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 889    Accepted Submission(s): 550

Problem Description
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use these sets to combinate the new set,how many different new set you can get.The given sets can not be broken.
 

 

Input
There are several cases.For each case,the first line contains two positive integer n and m(1<=n<=100,1<=m<=14).Then the following n lines describe the n sets.These lines each contains k+1 positive integer,the first which is k,then k integers are given. The input is end by EOF.
 

 

Output
For each case,the output contain only one integer,the number of the different sets you get.
 

 

Sample Input
4 4
1 1
1 2
1 3
1 4
2 4
3 1 2 3
4 1 2 3 4
 

 

Sample Output
15
2
 

 

Source
 

 

Recommend
gaojie
 
 
 
2013-03-24
 
 

这题就是用一个二进制数保存一个集合的元素 比如一个集合中有两个元素 1 3 那就用5 (101)表示这个集合

就是用0 1 来表示这个集合中一个数存不存在 再比如 一个集合有 三个元素 1 4 5 就在这几个位子上标为1,那就

用25 (11001)来表示这个集合!在借助于位运算的或( | )就可已达到合并集合的目的,比如一个集合(1 4 )

和一个集合(1 2 3)进行合并 那就是 (9)1001 | 111(7)=1111 就是15 这样就将重复的部分覆盖了。新的集合就用15来表示!最大就是(11111111111111)2^15-1来表示一个集合!

 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int n,m;
int mark[1<<15];

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        int k,x;
        memset(mark,0,sizeof(mark));
        while(n--){
            int tmp=0;
            scanf("%d",&k);
            while(k--){
                scanf("%d",&x);
                tmp |= 1<<(x-1);
            }
            mark[tmp]=1;
            for(int i=1;i<=(1<<14)-1;i++)
                if(mark[i])
                    mark[tmp|i]=1;
        }
        int ans=0;
        for(int i=0;i<(1<<14);i++)
            ans+=mark[i];
        printf("%d\n",ans);
    }
    return 0;
}

 

你可能感兴趣的:(number)