hduoj 3006 The Number of set

hduoj 3006 The Number of set

 

The Number of set

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

1 4 4
2 1 1
3 1 2
4 1 3
5 1 4
6 2 4
7 3 1 2 3
8 4 1 2 3 4

Sample Output

1 15
2 2

原题链接:点击

 

思路

这题真是让我看到了位运算和状压压缩的神奇之处!这题就是用一个二进制数保存一个集合的元素 比如一个集合中有两个元素   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来表示一个集合!

AC代码:

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<cstring>
5 #include<queue>
6 #include<map>
7 #include<algorithm>
8 using namespace std;
9 int a[1<<15];
10 int main()
11 {
12     int i,j,n,m;
13     while(~scanf("%d%d",&n,&m))
14     {
15         memset(a,0,sizeof(a));
16         int k,x,y;
17         int ans = 0;
18         while(n--)
19         {
20             scanf("%d",&k);
21             y = 0;
22             while(k--)
23             {
24                 scanf("%d",&x);
25                 y = y|(1<<x-1);
26             }
27             a[y] = 1;
28             for(i = 0;i <= 1<<14;i ++)
29             {
30                 if(a[i])
31                     a[i|y] = 1;
32             }
33         }
34         for(i = 1;i <= 1<<14;i ++)
35             if(a[i]) ans++;
36         printf("%d\n",ans);
37     }
38 }
posted @ 2013-07-13 12:51  风儿-zsj  阅读(134)  评论(0编辑  收藏  举报