poj 2441 Arrange the Bulls(状压DP入门)

本文介绍了使用状压DP解决农夫约翰如何安排公牛在特定牛棚打篮球的问题,包括输入输出规范、代码实现及案例分析。
Arrange the Bulls
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 3509 Accepted: 1344

Description

Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because they believe that the others are all very weak. Farmer Johnson has N cows (we number the cows from 1 to N) and M barns (we number the barns from 1 to M), which is his bulls' basketball fields. However, his bulls are all very captious, they only like to play in some specific barns, and don’t want to share a barn with the others. 

So it is difficult for Farmer Johnson to arrange his bulls, he wants you to help him. Of course, find one solution is easy, but your task is to find how many solutions there are. 

You should know that a solution is a situation that every bull can play basketball in a barn he likes and no two bulls share a barn. 

To make the problem a little easy, it is assumed that the number of solutions will not exceed 10000000.

Input

In the first line of input contains two integers N and M (1 <= N <= 20, 1 <= M <= 20). Then come N lines. The i-th line first contains an integer P (1 <= P <= M) referring to the number of barns cow i likes to play in. Then follow P integers, which give the number of there P barns.

Output

Print a single integer in a line, which is the number of solutions.

Sample Input

3 4
2 1 4
2 1 3
2 2 4

Sample Output

4


刚开始学习状压DP,有理解不对的还忘指正,谢谢。

参考的代码:http://blog.csdn.net/magicnumber/article/details/6647980

参考资料:http://www.hankcs.com/program/algorithm/poj-2441-arrange-the-bulls.html

我对代码的理解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int map[25][25];
int dp[(1<<20)+100];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
       int p;
       memset(map,0,sizeof(map));
       for(int i=1;i<=n;i++)
       {
          scanf("%d",&p);
          for(int j=1;j<=p;j++)
          {
              int x;
              scanf("%d",&x);
              x--;//1移x-1位就是第x个1
              map[i][x]=1;
          }
       }
       if(n>m)
       {
           printf("0\n");
           continue;
       }
       memset(dp,0,sizeof(dp));
       dp[0]=1;//全部都不选
       for(int i=1;i<=n;i++)
       {
           for(int j=(1<<m)-1;j>=0;j--)//枚举子集
           {
               if(!dp[j])//如果该子集不存在,跳过
               continue;
               for(int k=0;k<m;k++)//向子集中添加元素
               {
                   if(((1<<k)&j)!=0)//该元素已经在子集中了就跳过
                   continue;
                   if(map[i][k]==0)//不能取该元素
                   continue;
                   int temp=((1<<k)|j);//添加元素后的新的值
                   dp[temp]+=dp[j];//添加元素
               }
               dp[j]=0;//这个子集就不存在了。
           }
       }
       long long ans=0;
       for(int i=0;i<(1<<m);i++)
       ans+=dp[i];
       printf("%I64d\n",ans);
    }
    return 0;
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值