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

2009 Multi-University Training Contest 11 – Host by HRBEU题目链接:?pid=3006题目大意:给你n个集合,问用这些集合合并能组成多少新的集合题目分析:好题,因为m只有14,我们可以采用状态压缩,1表示该位置有数字,0表示没有,,例如1 2 3这个集合,111代表其状态,我们先把所给集合的状态都用hash存下来,然后通过让它们进行与运算,得到的结果也用hash存下来,最后只要通过hash计算一下有多少种状态即可#include <cstdio>#include <cstring>int const MAX = 1 << 15;bool hash[MAX];int main(){int n, m, len = 1 << 14;while(scanf("%d %d", &n, &m) != EOF){int cnt = 0;memset(hash, false, sizeof(hash));while(n–){int k, get, cur = 0;scanf("%d", &k);while(k–){scanf("%d", &get);cur = cur | (1 << (get – 1));}hash[cur] = true;for(int i = 0; i <= len; i++)if(hash[i])hash[i | cur] = true;}for(int i = 0; i <= len; i++)if(hash[i])cnt++;printf("%d\n", cnt);}}

旅行要学会随遇而安,淡然一点,走走停停,

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

相关文章:

你感兴趣的文章:

标签云: