hdu 3006

              题目意思是给定一些集合,求这些集合所能组合的集合个数。

            解法是dfs+剪枝+状态压缩hash。用二进制来保存该集合的元素情况,当dfs到不存在的集合情况时才继续搜索状态。

 

           

#include<cstdio>
#include<cstring>
#include<iostream>
#include<utility>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;
const int inf=1<<29;
const int M=1100000;
const int N=300;
const double eps=1e-8;
bool Hash[M];
int a[N];
int ans;
int n,m;
void dfs(int u,int temp)
{
	for(int v=u+1;v<n;v++)
	{
		if(!Hash[(temp|a[v])])
		{
			Hash[(temp|a[v])]=1;
			ans++;
			dfs(v,(temp|a[v]));
		}
	}
}

int main()
{
	while(scanf("%d%d",&n,&m)==2)
	{
		ans=0;
		for(int i=0;i<n;i++)
		{
			int k,b;
			a[i]=0;
			scanf("%d",&k);
			while(k--)
			{
				scanf("%d",&b);
				a[i]|=(1<<b);
			}
		}
		for(int i=0;i<=(1<<(m+1));i++)
			Hash[i]=0;
		for(int i=0;i<n;i++)
		{
			if(!Hash[a[i]])
			{
				Hash[a[i]]=1;
				ans++;
			}
			for(int j=i+1;j<n;j++)
			{
				if(!Hash[(a[i]|a[j])])
				{
					Hash[(a[i]|a[j])]=1;
					ans++;
					dfs(j,(a[i]|a[j]));
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值