题目链接:

http://poj.org/problem?id=2441

Arrange the Bulls

Time Limit: 4000MS
Memory Limit: 65536K
#### 问题描述
> 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.

输入

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.

输出

Print a single integer in a line, which is the number of solutions.

样例输入

3 4

2 1 4

2 1 3

2 2 4

样例输出

4

题意

有n只牛,m个体育场,每只牛只会去若干个自己喜欢的体育场,现在要分配n只牛到n个不同的体育场,问总共有多少种分配方案。

题解

dp[i][j]表示分配前i只牛在状态为j的体育场的总方案数。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef int LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9; const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=20; int dp[2][1<<maxn];
///sumv统计二进制中1的个数
int sumv[1<<maxn];
int n,m; bool mp[maxn][maxn]; void pre(){
clr(sumv,0);
rep(i,0,(1<<maxn)){
rep(j,0,maxn){
if(i&(1<<j)) sumv[i]++;
}
}
} void init(){
clr(mp,0);
} int main() {
pre();
while(scf("%d%d",&n,&m)==2&&n) {
init();
rep(i,0,n){
int cnt; scf("%d",&cnt);
while(cnt--){
int v; scf("%d",&v);
v--;
mp[i][v]=true;
}
} int pre=0,cur=1;
clr(dp[cur],0); ///初始化
for(int j=0;j<m;j++){
if(mp[0][j]){
dp[cur][1<<j]=1;
}
} for(int i=1;i<n;i++){
swap(pre,cur);
clr(dp[cur],0);
for(int j=0;j<m;j++){
if(mp[i][j]==0) continue;
for(int k=0;k<(1<<m);k++){
if(k&(1<<j)) continue;
dp[cur][k^(1<<j)]+=dp[pre][k];
}
}
} int ans=0;
for(int i=0;i<(1<<m);i++){
if(sumv[i]==n) ans+=dp[cur][i];
} prf("%d\n",ans); }
return 0;
} //end-----------------------------------------------------------------------

POJ 2441 Arrange the Bulls 状压dp的更多相关文章

  1. poj 2441 Arrange the Bulls

    Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 5427   Accepted: 2069 ...

  2. POJ 1185 炮兵阵地(状压DP)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26426   Accepted: 10185 Descriptio ...

  3. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  4. POJ 2411 Mondriaan's Dream ——状压DP 插头DP

    [题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...

  5. poj 2288 Islands and Bridges ——状压DP

    题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...

  6. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  7. POJ 1185 炮兵阵地 (状压DP)

    题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...

  8. 动态规划晋级——POJ 3254 Corn Fields【状压DP】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...

  9. POJ 1185 炮兵阵地 【状压DP】

    <题目链接> 题目大意: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...

随机推荐

  1. JQuery的ajax函数执行失败,alert函数弹框一闪而过

    先查看<form>标签是否有action属性,如果没有,并且最后<button>标签的type属性为'submit‘时,默认提交位置就是当前页面 如果在页面右键检查,点击网络, ...

  2. laravel 5.5 《电商实战 》安装应用

    最近开始接触电商业务.公司打算采用lavarel做后端的开发,出于学习成本和时间的考虑.自己找到了一个不错的收费教程.这段时间会同步更新,分享自己的学习过程. 自己的开发环境,mac+nginx+my ...

  3. 六、Delphi10.3通过Json.Serializers单元对大量数据序列化

    一.参考我之前的博客,Delphi可以很方便的把类和结构体转换成JSON数据,但是数据量大了,就会非常之慢,1万条数据需要20秒左右.如果引用Serializers单元,那么100万数据只需要4秒左右 ...

  4. 20155239 2017-11-19 实现mypwd(选做,加分)

    20155239 2017-11-19 实现mypwd(选做,加分) 题目和要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd ...

  5. 20155327 实验三 敏捷开发与XP实践

    20155327 实验三 敏捷开发与XP实践 实验内容 任务一 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装al ...

  6. 10 star组件之分页, search模糊查询, action批量处理

    1.分页组件高阶 1.分页的class形式(有bug,请看下面的) """ 自定义分页组件 """ class Pagination(obj ...

  7. 【LG3722】[HNOI2017]影魔

    [LG3722][HNOI2017]影魔 题面 洛谷 题解 先使用单调栈求出\(i\)左边第一个比\(i\)大的位置\(lp_i\),和右边第一个比\(i\)大的位置\(rp_i\). 考虑\(i\) ...

  8. TMS320VC5509串口通信

    1. 串口通信使用MCBSP外设的DX1,DRA引脚 很多同学喜欢把这个MCBSP驱动音频芯片TLV320AIC23,同时也作为串口,那么一般用的拨码开关去选择,反正自己看着拨一下 2. 遇到的一个问 ...

  9. bzoj 5301: [Cqoi2018]异或序列

    蛤?这一年cqoi的题这么水???? 这不就是个sb莫队吗 这样写怕是会被打死,,, 注意\(a_x\ XOR a_{x+1}\ XOR\ ...\ a_{y}=s_{x-1}\ XOR\ s_y\) ...

  10. idea中xml打开方式变成file,改回来

    原文:https://blog.csdn.net/u012903926/article/details/80682885 创建了一个test文件,用的是普通text打开方式,然后你修改文件为test. ...