题意很简单,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. Tornado模板配置

    #!/usr/bin/env python # -*- coding:utf-8 -*- #tornado模板配置 import tornado.ioloop import tornado.web c ...

  2. Arduino 003 Ubuntu(Linux) 系统下,如何给板子烧写程序

    Ubuntu/Linux 系统下,如何给Arduino板子烧写程序 使用的虚拟机软件:VMware 11 我的Ubuntu系统:Ubuntu 14.04.10 TLS Arduino 软件的版本:Ar ...

  3. 算法Sedgewick第四版-第1章基础-016一list

    import java.util.Iterator; import java.util.NoSuchElementException; public class List<Item> im ...

  4. About English Web Site Font

    Which font will you choose when develop one website! Actually , I have no idea about this! If you ha ...

  5. KindEditor 销毁与自动高度冲突解决

    前提准备情况: KindEditor(KE)  +  easyUI 1.通过 EasyUI.Window 打开一个窗口,窗口中包含一个 KE编辑器:在次打开WIndow 的时候 KE会出现编辑器里面的 ...

  6. File类和Directory类

    File类和Directory类分别用来对文件和各种目录进行操作,这两类可以被实例化,但不能被其他类集成. 1. File类(静态类) File类支持对文件的基本操作,它包括用于创建.复制.删除.移动 ...

  7. 机器学习基石笔记:11 Linear Models for Classification、LC vs LinReg vs LogReg、OVA、OVO

    原文地址:https://www.jianshu.com/p/6f86290e70f9 一.二元分类的线性模型 线性回归后的参数值常用于PLA/PA/Logistic Regression的参数初始化 ...

  8. UI界面相关

    在开发中有些控件或者控件显示的属性需要经常设置,但是又是万变不离其中,经常写着一样的代码会显得冗余,不利于阅读.这里做了简化. 1.UI控件 2.颜色管理 3.图片管理 4.字体选择

  9. 第一个spring MVC

    1.导包 Spring所有包 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web- ...

  10. git 命令总结(转)

    结构图: <1> Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库   # 在当前目录新建 ...