Arrange the Bulls [POJ2441] [状压DP]
题意
n头牛,m个房间,每头牛有自己喜欢的房间,问每头牛都住进自己喜欢的房间有多少种分配方法?
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
Analysis
首先,这种数据我们很容易想到是状压DP
我们可以比较轻松的写出状态转移方程
if(status&(1<<room)==0)
dp[i][status|(1<<room)]+=dp[i-1][status];
但是我们发现这样是会MLE的,我们就可以采用滚动数组,或是直接上一维
一维要注意status从大到小循环,因为是每次小的更新大的,status用完之后要清零。
Code
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
using namespace std;
int n,m,ans;
int a[][];
int dp[];
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
n=read(),m=read();
rep(i,,n)
{
a[i][]=read();
rep(j,,a[i][])
a[i][j]=read();
}
dp[]=;
rep(i,,n)
{
per(j,(<<m)-,)
{
if(!dp[j]) continue;
rep(k,,a[i][])
{
if(!(j&(<<(a[i][k]-))))
{
dp[j|(<<(a[i][k]-))]+=dp[j];
}
}
dp[j]=;
}
}
int ans=;
per(j,(<<m)-,) ans+=dp[j];
cout<<ans;
return ;
}
最新文章
- 逻辑回归算法的原理及实现(LR)
- Play jQuery with Node.js
- 基于Vue封装分页组件
- Bootstrap左侧下拉三级菜单
- Android开发--Activity的创建
- JMeter使用技巧
- 手机开发类型jquery的框架Zepto(API)
- 397. Integer Replacement
- uva 1331 - Minimax Triangulation(dp)
- [题解]bzoj 3223 文艺平衡树
- 用Python让单片机“行动”起来——MicroPython实战入门篇
- 探索ArrayList自动改变size真相
- Treeview 丢失焦点后依然高亮 SelectedNode
- Windows下Apache的下载安装启动停止
- 递归,re,time,random
- SQL Server2012安装流程
- multiprocess模块
- 表型数据(Phenotype Data)基本概念
- TensorRT 不支持Tensorflow的操作有如下
- 5F - Coin Change
热门文章
- Vue(小案例_vue+axios仿手机app)_实现用户评论
- python 网络编程之TCP传输&;粘包传输
- Java编码中出现的乱码问题
- [Luogu P3295][SCOI 2016]萌萌哒
- Newton&#39;s Dark Secrets《牛顿探索》
- EffectiveC++ 第6章 继承与面向对象设计
- H5取经之路——添加hover实现特定效果
- MySQL入门(参考官网)
- Linux 踩过的坑系列-01
- 设计模式九: 观察者模式(Observer Pattern)