题意很简单,n头牛,m个位置,每头牛有各自喜欢的位置,问安排这n头牛使得每头牛都在各自喜欢的位置有几种安排方法。

2000MS代码:

#include <cstdio>
#include <cstring>
int dp[(<<)+];
int one[( << ) + ];
//用来数出状态为i时1的个数,具体到这个题中就是
//状态为i时有多少头牛已经安排好牛棚
void CountOne(int m)
{
for(int i=; i< ( << m); ++i)
{
int num=;
for(int j=; j< m; ++j)
{
if( (i & ( << j)) != )//i的第j位置是否为一
++num;
}
one[i] = num;
}
}
int main()
{
// freopen("in.cpp","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
CountOne(m);
memset(dp,,sizeof(dp));
dp[] = ; //一头牛都没有安排,状态为0的满足条件的方案数为1
for(int i=; i<=n; ++i)
{
int cnt; //每头牛喜欢住的牛棚数
scanf("%d",&cnt);
while(cnt--)
{
int k; //该牛棚编号
scanf("%d",&k);
--k;//使得牛棚编号为 0 ~ m-1
for(int j=; j< ( << m); ++j)
{
if((j & ( << k)) != && one[j] == i) //这个状态已经安排好了i头牛,且第k个牛棚安排的是第i头牛
dp[j] += dp[j-(<<k)];
}
}
}
// 最终结果为安排了n头牛的状态满足条件的方案数的总和
int ans=;
for(int j=; j< ( << m ); ++j)
{
if(one[j] == n)
{
ans += dp[j];
}
}
printf("%d\n",ans);
return ;
}

90MS

#include <stdio.h>
#include <string.h> int map[][];
int now[(<<)+]; int main()
{
int i,j,n,m,k,x,p,ret;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,,sizeof(map));
for (i=;i<n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
x--;//0~m-1编号
map[i+][x]=;
}
}
if (n>m)
{
printf("0\n");
continue;
}
memset(now,,sizeof(now));
now[]=;
for (i=;i<n;i++)
{
for (j=(<<m)-;j>=;j--)
{
if (now[j]==) continue;
for (k=;k<m;k++)
{
if ((j & (<<k))!=) continue;//判断j的第k的位置为1
if (map[i+][k]==) continue;//在k的棚没有位置
p=(j | (<<k));//j的第k的位置变1
now[p]+=now[j];
}
now[j]=;
}
}
ret=;
for (i=;i<(<<m);i++)
{
ret+=now[i];
}
printf("%d\n",ret);
}
return ;
}

POJ 2441 Arrange the Bulls(状态压缩DP)的更多相关文章

  1. POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)

    推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...

  2. POJ 2441 Arrange the Bulls 状压dp

    题目链接: http://poj.org/problem?id=2441 Arrange the Bulls Time Limit: 4000MSMemory Limit: 65536K 问题描述 F ...

  3. POJ 1691 Painting a Board(状态压缩DP)

    Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...

  4. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  5. POJ 2686_Traveling by Stagecoach【状态压缩DP】

    题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间. 分析: 状态压 ...

  6. poj 2441 Arrange the Bulls(状态压缩dp)

    Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...

  7. poj 2441 Arrange the Bulls

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

  8. poj 2411 Mondriaan's Dream_状态压缩dp

    题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...

  9. poj 1185 炮兵阵地 [经典状态压缩DP]

    题意:略. 思路:由于每个大炮射程为2,所以如果对每一行状态压缩的话,能对它造成影响的就是上面的两行. 这里用dp[row][state1][state2]表示第row行状态为state2,第row- ...

  10. POJ 1038 Bug Integrated Inc(状态压缩DP)

    Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...

随机推荐

  1. Developer tools

    20. Developer tools Spring Boot includes an additional set of tools that can make the application de ...

  2. nodejs的POST请求

    http://blog.csdn.net/puncha/article/details/9015317 Nodejs 发送HTTP POST请求实例 2013-06-03 17:55 71745人阅读 ...

  3. webview加载url出现空白页面,有些页面没问题

    用webview加载url出现空白页,测试后把百度,Github之类的url传进去都没问题,后来发现是因为布局的原因,因为webview对不同的网站兼容性没有那么强,特别是现在出现的各种前端布局,没法 ...

  4. php学习笔记-continue和break

    这两个关键字经常被用在循环中,但作用是完全不同的. 在循环中遇到continue这个单词的时候一定要理解为skip,跳过或者略过,啥意思?就是跳过本次循环,后面的循环继续走起来,老铁. break是说 ...

  5. 分享常用的GoLang包工具

    包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqt ...

  6. Mac安装破解版Office 2016办公软件

    一.相关软件 Microsoft Office 2016 For Mac Cracker 破解工具 资源地址(链接:https://pan.baidu.com/s/1Z5CIv-XbxS08MniYN ...

  7. Bulma 源码结构

    源码基于 Bulma 0.4.0 版本. 一.入口文件 bulma.sass bulma.sass 是 Bulma 使用 SASS 编译的入口文件. sass bulma.sass css/bulma ...

  8. 数据结构--树--AVL树

    详情查看:http://www.cnblogs.com/skywang12345/p/3577360.html

  9. Spring JDBCTemplate配置使用

    一.开发环境 Windows 10 IntelliJ IDEA 2016.1 旗舰版 JDK1.8 二.项目和数据库结构 项目结构: 数据库(MySQL 5.5.39): /* Navicat MyS ...

  10. Joda Time 使用

    Joda Time 使用 对于系统的一些时间操作很是不方便,为了方便转化,有时候用date,有时候用timestmp,有时候用calendar,忍不住想更改了. 但是任务巨大,先把笔记收藏了,后面有机 ...