POJ 2441 Arrange the Bulls

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

题意:有N头牛,每头牛都有自己喜欢的barn 而且一个barn只能有一头牛,求给这些牛分配barn 共有多少种方法

思路:状态压缩DP,
dp[i][j] 表示在j的状态下放第j头牛的方法数
要用到滚到数组 因为dp[20][1<<20] 的数组会MLE。
详见代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;

int dp[2][1<<20];
int mp[25][25];
int n, m;
int main(void)
{
//  freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
    int i, j, k, t, S, c, ans;
    while (~scanf("%d %d", &n, &m))
    {
        memset(mp, 0, sizeof(mp));
        for (i = 1; i <= n; i++){
            scanf("%d", &t);
            for (j = 1; j <= t; j++){
                scanf("%d", &k);
                mp[i][k] = 1;
            }
        }
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;   // 初始化1 
        for (i = 1; i <= n; i++){
            for (S = 0; S < (1<<m); S++){
                c = i & 1;
                if (!dp[1-c][S]) continue; // 不加这个会T...
                for (j = 0; j < m; j++){
                    if (S & (1<<j)) continue; // 如果当前状态第j个已经有牛了 pass 
                    if (!mp[i][j+1]) continue; // 如果当前牛不喜欢第j个barn  pass 
                    dp[c][S|(1<<j)] += dp[1-c][S];
                }
            }
            memset(dp[1-c], 0, sizeof(dp[1-c])); // 滚动数组
                // 因为放第i头牛方法数只与放第i-1头牛方法数有关
                // 所以就可以只保存第i-1头在各个状态下的方法数
        }
        ans = 0;
        for (i = 0; i < (1<<m); i++)
            ans += dp[n&1][i];
        printf("%d\n", ans);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值