Arrange the Bulls
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5289   Accepted: 2033

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

题意:每只牛只想在特定的房间里玩,每个房间只能有一只牛,问你能够满足以上条件的情况有几种。

代码:

 //#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define inf 0x3f3f3f3f
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int f[<<],a[][];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int x;
memset(a,, sizeof(a));
memset(f,, sizeof(f));
for(int i=;i<n;i++){
ci(x);
for(int j=;j<x;j++){
int y;
ci(y);
a[i][y-]=;
}
}
if(n>m) {
puts("");
continue;
}
f[]=;
for(int i=;i<n;i++){//枚举n只牛
for(int j=(<<m)-;j>=;j--){//枚举每一个集合
if(f[j])//如果集合存在
{
for(int k=;k<m;k++){//枚举每个房间
if((j&(<<k))!=||!a[i][k]) continue;//集合已用此房间或牛不喜欢此房间
int S=(<<k)|j;//牛进入房间得到新的集合
f[S]+=f[j];
}
}
f[j]=;//枚举的集合变为新集合,旧集合需要消除
}
}
ll ans=;
for(int i=;i<(<<m);i++) ans+=f[i];//留下满足条件的
pl(ans);
}
return ;
}

POJ 2441 状压DP的更多相关文章

  1. POJ 3254 (状压DP) Corn Fields

    基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...

  2. poj 1170状压dp

    题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...

  3. POJ 3254 状压DP

    题目大意: 一个农民有一片n行m列 的农场   n和m 范围[1,12]  对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...

  4. POJ 2411 状压DP经典

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16771   Accepted: 968 ...

  5. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  6. poj 1185(状压dp)

    题目链接:http://poj.org/problem?id=1185 思路:状态压缩经典题目,dp[i][j][k]表示第i行状态为j,(i-1)行状态为k时最多可以放置的士兵个数,于是我们可以得到 ...

  7. poj 2923 状压dp+01背包

    好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...

  8. Islands and Bridges(POJ 2288状压dp)

    题意:给你一个图和每个点的价值,边权值为连接两点权值的积,走哈密顿通路,若到达的点和上上个点相连则价值加三点乘积,求哈密顿通路的最大价值,和最大价值哈密顿通路的条数. 分析:开始看这个题很吓人,但想想 ...

  9. Hie with the Pie(POJ 3311状压dp)

    题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...

随机推荐

  1. HTML图片映射实用

    大图的不同点击区域实现不同的超链接: <img src="planets.gif" width="145" height="126" ...

  2. C++ Knowledge series 4

    Programming language evolves always along with Compiler's evolvement The Semantics of Function C++ s ...

  3. Android 通过接口的方式去调用服务里面的方法

    public class MainActivity extends AppCompatActivity { private MyConn conn; private Iservice myBinder ...

  4. powershell解决win10开始菜单和通知中心无法打开

    然后通过 Ctrl + Shift + Esc 弹出任务管理器点击文件-->运行新任务 在打开的填写框里面输入 "powershell"同时勾选下方的"以管理员身份 ...

  5. vos限制客户呼出时间

    问题: 公司希望自己的卡线晚上 21:00-24:00    早上00:00-7:30不能打出电话,以防遭投拆, 那么如何设置? 方法: 找到客户使用的落地网关, 落地网关——补充设置——

  6. C#学习基础,面向对象的三大特征

    学习C#编程,相信大家除了经常接触的是hello world之外,更多的是进一步的去熟悉这门语言的基本特征,以及有哪些概念是我们必要掌握了解的,相信大家都是会知道面向对象的三大特性分别是:封装,继承, ...

  7. simotion ST编程,变量和程序的模块化

    通过这几年的实践,对simotion的程序结构规划有了一些新的理解.工程师在面对项目时,经常有几个问题比较困惑. 目前面对的问题 1)如何理解其他人编写的程序 2)如何从程序中找到可以移植的部分 传统 ...

  8. golang 2行代码在基于arm linux的树莓派、orangepi上运行http web服务

    go语言(golang)简化了跨平台交叉编译步骤,支持在windows系统下交叉编译基于arm+linux平台的应用,运行时无需其它依赖库.以下以一个简单的http server为例,先上源码: ** ...

  9. naive bayes classifier in data mining

    https://www-users.cs.umn.edu/~kumar001/dmbook/slides/chap4_naive_bayes.pdf  -- textbook https://www. ...

  10. nodejs 的一些PHP函数库

    http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...