The Number of set
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 897 Accepted Submission(s): 555
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这题真是让我看到了位运算和状压DP的神奇之处!
这题就是用一个二进制数保存一个集合的元素 比如一个集合中有两个元素 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代码:#include <iostream> #include <cstring> using namespace std; int main() { int m,n,k,a; int s[1<<15]; //2的15次方 while(cin>>n>>m) { int ans=0; memset(s,0,sizeof(s)); while(n--) { int set=0; cin>>k; while(k--) { cin>>a; set=set|(1<<(a-1)); } s[set]=1; for(int j=0;j<=(1<<14);j++) { if(s[j]) s[set|j]=1; } } for(int i=0;i<=1<<14;i++) { if(s[i]) ans++; } cout<<ans<<endl; } return 0; }