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
 
解题思路:简单状压DP
dp[i][j]:表示前i头牛状态为j时的方案数,需要对j枚举的状态数进行优化,否则很容易超时。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = (1<<20)+10;
bool G[21][21];
int dp[2][maxn];
int ms[21];
int n, m, p;
vector<int> tran[21];

int count_bit(int x) {
    int bit = 0;
    while(x) {
        if(x&1) bit++;
        x >>= 1;
    }
    return bit;
}

void init() {
    memset(G, false, sizeof(G));
    for(int i = 0; i < 21; ++i) {
        ms[i] = 0;
        tran[i].clear();
    }
    for(int i = 0; i < (1<<m); ++i) {
        tran[count_bit(i)].push_back(i);
    }
}

int main() {

    //freopen("aa.in", "r", stdin);

    int v;
    int id;
    while(scanf("%d %d", &n, &m) != EOF) {
        id = 0;
        init();
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &p);
            for(int j = 1; j <= p; ++j) {
                scanf("%d", &v);
                G[i][v-1] = true;
                ms[i] |= (1<<(v-1));
            }
        }
        if(n > m) {
            printf("0\n");
            continue;
        }
        memset(dp, 0, sizeof(dp));
        dp[id][0] = 1;
        /* 对j的枚举进行优化 */
        for(int i = 1; i <= n; ++i) {
            int s = tran[i].size();
            for(int j = 0; j < s; ++j) {
                dp[id^1][tran[i][j]] = 0;
                if(tran[i][j]&ms[i]) {
                    for(int k = 0; (1<<k) <= tran[i][j]; ++k) {
                        if((1<<k)&tran[i][j] && G[i][k]) {
                            dp[id^1][tran[i][j]] += dp[id][tran[i][j]^(1<<k)];
                        }
                    }
                }
            }
            id ^= 1;
        }
        /*
        for(int i = 1; i <= n; ++i) {
            for(int j = 0; j < (1<<m); ++j) {
                dp[id^1][j] = 0;
                if(j&ms[i]) {
                    for(int k = 0; (1<<k) <= j; ++k) {
                        if((1<<k)&j && G[i][k]) {
                            dp[id^1][j] += dp[id][j^(1<<k)];
                        }
                    }
                }
            }
            id ^= 1;
        }
        */
        int ans = 0;
        for(int i = 0; i < (int)tran[n].size(); ++i) {
            ans += dp[id][tran[n][i]];
        }
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值