描述

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. Elipse 无法启动问题(转)

    来源: <http://www.cnblogs.com/coding-way/archive/2012/10/17/2727481.html> 当选择完workspace之后,eclips ...

  2. 当css样式表遇到层2

    9.定制层的display属性:层的表现是通过框这种结构来实现的.框可以是块级对象也可以是行内对象. Display属性就是用来控制其中内容是块级还是行级.定义为block则为kuai块级,inlin ...

  3. css中常见中文字体的英文名称

    曾经看过一些文章,建议CSS中字体应用英文来替代,但一直未引起我重视.最近官网改版,今天同事测试发现Mac的Safari总是显示宋体 → → 修改font-family:"微软雅黑" ...

  4. TRUNCATE DELETE DROP 区别

    TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源 ...

  5. abp zero mysql版正式发布

    AbpZero-MySql aspnet-zero-1.12.0的mysql版本来啦.跟mssql版一样的功能,一样的代码. 获取源码

  6. AJPFX总结多线程编程的注意事项

    多线程编程的注意事项          1.明确目的,为什么要使用多线程?如果是由于单线程读写或者网络访问(例如HTTP访问互联网)的瓶颈,可以考虑使用线程池.如果是对不同的资源(例如SOCKET连接 ...

  7. 【转】10种简单的Java性能优化

    10种简单的Java性能优化 2015/06/23 | 分类: 基础技术 | 14 条评论 | 标签: 性能优化 分享到: 本文由 ImportNew - 一直在路上 翻译自 jaxenter.欢迎加 ...

  8. 014、BOM与DOM对象的应用

    Screen屏幕对象 Width:屏幕的宽度 Height:屏幕的高度 availWidth:屏幕的有效宽度(不含任务栏) availHeight:屏幕的有效高度(不含任务栏) colorDepth: ...

  9. f# mathprovider

    http://mathprovider.codeplex.com/ http://mathnetnumerics.codeplex.com/releases/view/110750 http://py ...

  10. ios MD5大小写加密

    #import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...