描述

HaHa is so happy today, he is going to participate the 7th Hunan University Programming Contest. He woke up in the morning, and wanted to reach Hunan University as soon as possible, but he realized that he still has N things to do before going on his journey.
At first, HaHa thought there must have N! (The factorial of N) ways to get everything done, however, he soon found that this was impossible at all, for the work has some annoying restrictions: some things must be done before getting some other things done. Now HaHa is interested in the number of ways to get everything done, and he asks you for help, so your task is to find how many ways are there to finish his work.

输入

There are several test cases, each case contains several lines, and the first line of each case is two natural numbers N (that described above) and M ≤ 400 (for the total restrictions for the work).
The next M lines describes the restrictions, for each line, there is two positive integers A, B, for the A-th thing must be done before the B-th thing.
The input will finish with the end of file, input is guaranteed that 1 ≤ A, B ≤ N ≤ 17.

输出

For each the case, output one number: the ways to finish the work.

样例输入

3 2
1 3
2 3
2 2
1 2
2 1

样例输出

2
0

题目大意:

输入n,m,n代表完成的事的数量,接下来m行,每行输入a,b代表做b之前需要将a做完,求做完所有事的方案种数.

状压DP,dp[i]代表做完i所表示的二进制对应的事情有的方案数。例:dp[18]对应二进制10010代表做完第二和第五件事的方案数。

#include <cstdio>
#include <cstring>
using namespace std;
int n,m,pre[];
long long dp[<<];///17!爆int
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(pre,,sizeof pre);///pre[i]代表做事情i的前提
memset(dp,,sizeof dp);
for(int i=,x,y;i<=m;i++)
scanf("%d%d",&x,&y),pre[y]|=(<<(x-));
dp[]=;
for(int s=;s<(<<n);s++)
{
if(dp[s]==) continue;
for(int i=;i<=n;i++)
if((s&pre[i])==pre[i]&&(s&(<<(i-)))==)///s包含了i的所有前提,但不包括i
dp[s|(<<(i-))]+=dp[s];///将i加进去
}
printf("%I64d\n",dp[(<<n)-]);
}
return ;
}

HaHa's Morning(状压DP)的更多相关文章

  1. 状压dp Codeforces Beta Round #8 C

    http://codeforces.com/contest/8/problem/C 题目大意:给你一个坐标系,给你一个人的目前的坐标(该坐标也是垃圾桶的坐标),再给你n个垃圾的坐标,这个人要捡完所有的 ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  4. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  5. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  6. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  7. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  8. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  9. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

随机推荐

  1. 题解报告:hdu 2196 Computer(树形dp)

    Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...

  2. 164 Maximum Gap 最大间距

    给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值.尽量尝试在线性时间和空间复杂度情况下解决此问题.若数组元素个数少于2,则返回0.假定所有的元素都是非负整数且范围在32位有符号整数范围内. ...

  3. ionic之自定义图片

    一个好的app,必须都有很好的ui设计师来设计界面,增强客户的体验,表现自己本身公司的特色,但是,在ionic中有些是无法用img标签直接引入图片,只能通过设定的css之后引入css. 页面: < ...

  4. 高阶组件(Higher-Order Components)

    有时候人们很喜欢造一些名字很吓人的名词,让人一听这个名词就觉得自己不可能学会,从而让人望而却步.但是其实这些名词背后所代表的东西其实很简单. 我不能说高阶组件就是这么一个东西.但是它是一个概念上很简单 ...

  5. Android程序打包为APK

    Andriod安装包文件(Android Package),简称APK,后缀名为.apk. 1.生成未签名的安装包 Build -> Build Bundle(s)/APK(s) -> B ...

  6. 获取登陆信息 在created()方法中

    // 获取登录信息 public async InitUser() { await sj.globalVar.Init(true); this.params.unitId = sj.globalVar ...

  7. Android开发实现高德地图定位

    1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...

  8. Java replaceAll不区分大小写

    Java 中replaceAll如何忽略大小写呢? 方式一:在正则表达式前面添加(?i) @Test public void test_replaceAll33(){ String input = & ...

  9. swift Equatable 函数签名的测试

    struct Degoo:Equatable { var lex:String var pex:String static func == (left:Degoo, right:Degoo) -> ...

  10. Dart开发环境搭建

    一.SDK的安装与环境配置 1. 下载Dark SDK http://www.gekorm.com/dart-windows/ 2.  安装SDK 3.  配置环境变量(一般已经默认生成好了,这里可以 ...