Arrange the Bulls(poj2441)状态压缩dp

Arrange the Bulls
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 4317 Accepted: 1646

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

Source

题目的意思是有n头牛,m个篮球场,每头牛都有喜欢的篮球场(编号),每头牛都不愿意和其他牛在同一个篮球场,求共有多少种分配方法使每头牛都在自己喜欢的篮球场且这个篮球场只有一头牛。

题目很简单(是吗?并不觉得,简直要疯,第一次提交MLE,用滚动数组好多次样例都错,最坑的是和别人AC的代码几乎完全一样,竟然样例没过),状态压缩dp,dp[i][j]表示前i头牛状态为j时的方法数,对于任一个第i+1头牛喜欢的篮球场u,有dp[(i+1)&1][j|(1<<(u-1))]+=dp[i&1][j]。这样样例就过了,但是会MLE,采用滚动数组。i+1由i推,放在i-1的位置上,即采用dp[2][1<<maxn](i&1)。下面是AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define maxn 21
const int Mod=1e7;
vector<int> P[maxn];
int dp[2][1<<maxn];
int main()
{
    int n,m,p,x;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&p);
        for(int j=0;j<p;j++){
           scanf("%d",&x);
           P[i].push_back(x);
        }
    }
    memset(dp,0,sizeof(dp));
    for(int i=0;i<P[1].size();i++){
        int u=P[1][i];
        dp[1][1<<(u-1)]=1;
    }
    //dp[0][0]=1;
    for(int i=1;i<n;i++){
        for(int j=0;j<(1<<m);j++){
            if(dp[i&1][j]){
                for(int k=0;k<P[i+1].size();k++){
                    int u=P[i+1][k];
                    if(!(j&(1<<(u-1)))){
                        dp[(i+1)&1][j|(1<<(u-1))]+=dp[i&1][j];
                    }
                }
            }
        }
        memset(dp[i&1],0,sizeof(dp[i&1]));
    }
    int ans=0;
    for(int i=0;i<(1<<m);i++){
        ans+=dp[n&1][i];
        ans%=Mod;
    }
    cout<<ans<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值