(状态压缩DP) poj 2441

Arrange the Bulls

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 3709   Accepted: 1422

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

POJ Monthly,TN

题意:

有n头牛 m个帐篷,n头牛进入m个帐篷方案数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
using namespace std;
int n,m,mp[21][21];
int dp[(1<<20)+2];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        int x,y;
        scanf("%d",&x);
        while(x--)
        {
            scanf("%d",&y);
            y--;
            mp[i+1][y]=1;
        }
    }
    if(n>m)
    {
        printf("0\n");
        return 0;
    }
    dp[0]=1;
    for(int i=0;i<n;i++)
    {
        for(int j=(1<<m)-1;j>=0;j--)
        {
            if(dp[j]==0)
                continue;
            for(int k=0;k<m;k++)
            {
                if((j&(1<<k))!=0)
                    continue;
                if(mp[i+1][k]==0)
                    continue;
                dp[j|(1<<k)]+=dp[j];
            }
            dp[j]=0;
        }
    }
    int ans=0;
    for(int i=0;i<(1<<m);i++)
        ans+=dp[i];
    printf("%d\n",ans);
    return 0;
}

  

时间: 2024-11-08 20:24:50

(状态压缩DP) poj 2441的相关文章

(状态压缩DP) poj 2978

Colored stones Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1734   Accepted: 819 Description You are given a row of m stones each of which has one of k different colors. What is the minimum number of stones you must remove so that no

状态压缩dp poj 3254 hdu5045

近来感觉状态压缩dp的强大性(灵活利用了二进制运算很关键)...于是做了俩提来看看..毕竟队友是专业的dp,我只是管中窥豹下而已..日后有机会再与之玩耍玩耍...ps:如果上天再给我一次机会,当年我愿意选择状态dp而不是网络流(只针对目前比赛出题潮流) 经典问题,不相邻/禁点方案数问题.poj3254 #include<iostream> #include<cstdio> using namespace std; int n,m; int dp[5000][15]; int yu[

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

(状态压缩dp) poj 3254

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8729   Accepted: 4651 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

poj 2411 Mondriaan&#39;s Dream(状态压缩+dp)

 题意:用1*2砖块铺满n*m的房间. 思路转自:http://www.cnblogs.com/scau20110726/archive/2013/03/14/2960448.html 因为这道题输入范围在11*11之间,所以可以先打表直接输出.......... 状态压缩DP 经典覆盖问题,输入n和m表示一个n*m的矩形,用1*2的方块进行覆盖,不能重叠,不能越出矩形边界,问完全覆盖完整个矩形有多少种不同的方案 其中n和m均为奇数的话,矩形面积就是奇数,可知是不可能完全覆盖的.接着我们来看

POJ 3254 Corn Fields 状态压缩DP

题目链接:http://poj.org/problem?id=3254 思路:状态压缩DP,状态方程为dp[i][j] += (dp[i-1][k]) code: #include <stdio.h> #include <string.h> #define N 500 const int MOD = 100000000; int dp[15][N],ant[N],n,m,k,map[15]; bool ok(int x) { if(x&(x<<1))return

POJ 3691 (AC自动机+状态压缩DP)

题目链接:  http://poj.org/problem?id=3691 题目大意:给定N的致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题思路: 首先说一下AC自动机在本题中的作用. ①字典树部分:负责判断当前0~i个字符组成的串是否包含致病DNA,这部分靠字典树上的cnt标记完成. ②匹配部分:主要依赖于匹配和失配转移关系的计算,这部分非常重要,用来构建不同字符间状态压缩的转移关系(代替反人类的位运算). 这也是必须使用AC自动机而

poj 3254 Corn Fields ,状态压缩DP

题目链接 题意: 一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) state[i] 表示对于一行,保证不相邻的方案 状态:dp[i][ state[j] ]  在状态为state[j]时,到第i行符合条件的可以放牛的方案数 状态转移:dp[i][ state[j] ] =Sigma dp[i-1][state'] (state'为符合条