描述

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. drbd 配置

    DRBD(Distributed Replicated Block Device),DRBD 号称是 "网络 RAID",开源软件,由 LINBIT 公司开发.DRBD实际上是一种 ...

  2. C#方法拓展

    作用: “扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 要求: 1.拓展方法必须是在一个非嵌套.非泛型的静态类中定义.2.他至少有一个参数.3. ...

  3. Spring------自动化装配Bean(二)

    上一篇是基于 @ComponentScan自动装配Bean的实现,这一篇将通过java手动装配bean来实现. 手动装配相对于自动装配的优点: 可以自行定义Bean的各个属性. 添加额外的方法调度. ...

  4. CentOS Linux下MySQL 5.1.x的安装、优化和安全配置

    下载页面:http://dev.mysql.com/downloads/mysql/5.1.html#downloads 到页面底部,找到Source downloads,这个是源码版本,下载第1个T ...

  5. C#基础学习2

    变量与数据类型!

  6. 【前端】jq弹出一个透明小提示窗,然后逐渐消失

      function show_main(content) { var showWindow = '<div id="show_main" style="borde ...

  7. 【转】Iconfont

    Iconfont Iconfont 是指用字体文件取代图片文件,来展示图标.特殊字体等元素的一种方法.很多网站都会用它,比如手淘.新浪微博等. 在使用它之前,先来了解一下它的优缺点: 优点:(1)文件 ...

  8. 洛谷P3254 圆桌问题(最大流)

    题意 $m$个不同单位代表参加会议,第$i$个单位有$r_i$个人 $n$张餐桌,第$i$张可容纳$c_i$个代表就餐 同一个单位的代表需要在不同的餐桌就餐 问是否可行,要求输出方案 Sol 比较zz ...

  9. T4308 数据结构判断

    https://www.luogu.org/record/show?rid=2143639 题目描述 在世界的东边,有三瓶雪碧. ——laekov 黎大爷为了虐 zhx,给 zhx 出了这样一道题.黎 ...

  10. LibreOJ #119. 最短路 (堆优化dijkstra)

    题目描述 给一个 n(1≤2500≤n) n(1 \leq 2500 \leq n)n(1≤2500≤n) 个点 m(1≤6200≤m) m(1 \leq 6200 \leq m)m(1≤6200≤m ...