Arrange the Bulls POJ - 2441 (状压dp+滚动数组)

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[s][i]表示已选状态为s,且最后一个选了i的数目

但是这样空间会爆,所以直接dp[s],最后利用二进制1的个数关系统计下就行。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
int n,m;
int mp[30][30];
int dp[(1<<20)+5];
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
        {
            int k;
            scanf("%d",&k);
            for(int j=1;j<=k;j++)
            {   
                int x;
                scanf("%d",&x);
                x--;
                mp[i][x]=1;
            }
        }
        int sta=(1<<m)-1;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=0;i<n;i++)//1,2,3.。。。头牛的选法
        {
            for(int s=0;s<=sta;s++)
            {
                if(__builtin_popcount(s)==i)
                {
                    for(int k=0;k<m;k++)
                    {
                        if(!(s&(1<<k))&&mp[i][k])
                        {
                            dp[s|(1<<k)]+=dp[s];
                        }
                    }
                }
            }
        }
        int ans=0;
        for(int i=0;i<=sta;i++)
        {
            if(__builtin_popcount(i)==n)//满足选了n头牛的
            {
                ans+=dp[i];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81783314
今日推荐