poj 2441 Arrange the Bulls(状压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

题目大意:给出n个牛和m个场地,每个牛有几个喜欢的位置,每个场地只能有一个牛,安排每个牛去一个喜欢的场地玩,问总共有多少种方案。

我们可以把每个场地有无占用的情况看为一种状态,压缩成二进制数字s,我们可以从小到大枚举牛的数量,对于每一种数量枚举s的情况,只有当s中1的数量恰好等于i时,这种情况是合法的,然后我们可以在s多一个1的情况中加上这种情况扩展来的情况数。
最后我们只要统计所有s中1的数量等于n的情况就可以了。

__builtin_popcount()是g++中的一个函数,用于统计这个数字中二进制下1的个数。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
#define maxn 35
#define mod 2520

int n,m;
int dp[1<<21];
int g[25][25];

void solve()
{
    dp[0]=1;
    for(int i=0; i<n; i++)
    {
        for(int s=0; s<(1<<m); s++)
            if(__builtin_popcount(s)==i)
            {
                for(int k=0;k<m;k++)
                    if(!(s&(1<<k))&&g[i][k])
                    dp[s|(1<<k)]+=dp[s];
            }
    }
    int ans=0;
    for(int s=0;s<(1<<m);s++)
        if(__builtin_popcount(s)==n) ans+=dp[s];
    cout<<ans<<endl;
}

int main ()
{
    cin>>n>>m;
    CL(g);
    CL(dp);
    for(int i=0; i<n; i++)
    {
        int k;
        cin>>k;
        for(int j=0; j<k; j++)
        {
            int p;
            cin>>p;
            g[i][p-1]=1;
        }
    }
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值