描述

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. 堆参数-XMS 与-XMX的说明

    XMS : JVM初始分配的堆内存 XMX : JVM最大允许分配的堆内存,按需分配 堆内存分配: JVM初始分配的堆内存由-Xms指定,默认是物理内存的1/64: JVM最大分配的堆内存由-Xmx指 ...

  2. Vue nextTick 理解

    官网解释: 将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新.它跟全局方法 Vue.nextTick 一样,不同的是回调的 this 自动绑定到调用它的实例 ...

  3. 【C#】.net 发送get/post请求

    基础学习 /// <summary> /// Http (GET/POST) /// </summary> /// <param name="url" ...

  4. javascript回调函数那些事~

    什么是回调函数? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直 ...

  5. jquery实现上传图片及图片大小验证、图片预览效果代码

    jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...

  6. STM32&AT指令NBIOT模组

    #include "nbiot.h" #include "string.h" #include "stdlib.h" #include &q ...

  7. laravel的scout包安装及laravel-es包安装

    安装laravel/scout 作用:搜索驱动,可随时更换驱动,上层业务逻辑可不用改变 官网文档:https://laravel-china.org/docs/laravel/5.4/scout/12 ...

  8. 解决./mysql-bin.index’ not found (Errcode: 13)

    问题出现在升级php版本以后,网站无法连接数据库,phpMyAdmin无法登录: 然后尝试开启mysql,/etc/init.d/mysqld start ,提示: Starting MySQL. E ...

  9. mac osx上为qt应用生成debug symbol

    mac平台上,希望Qt编译的release程序也能包含debug symbol,这样出问题以后便于查找问题 开始按照http://doc.qt.io/qt-4.8/mac-differences.ht ...

  10. How `new’ operator works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator ...