K - Necklace

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:327680KB     64bit IO Format:%I64d & %I64u

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

Description

  One day , Partychen gets several beads , he wants to make these beads a necklace . But not every beads can link to each other, every bead should link to some particular bead(s). Now , Partychen wants to know how many kinds of necklace he can make. 

Input

It consists of multi-case .  Every case start with two integers N,M ( 1<=N<=18,M<=N*N )  The followed M lines contains two integers a,b ( 1<=a,b<=N ) which means the ath bead and the bth bead are able to be linked. 

Output

An integer , which means the number of kinds that the necklace could be.

Sample Input

3 3
1 2
1 3
2 3

Sample Output

2

题意:

给出N个珠子,要把他们连成串,每个珠子都要用上,有多少种连法。重点是dp状态转移方程。

代码:

 /*
给出的珠子要全部用上,因此只要最后的状态dp[i][j]中的j能够与第一个珠子相连就行,其中i表示取珠子
的状态,每多加一颗珠子他的dp数就是加上没加之前的dp数(重点!).
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n,m,a,b;
bool d[][];
long long dp[<<][]; //用int过不了
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
a--; //这里减一是为了后面写循环简便,也可以不减一
b--;
d[b][a]=;
d[a][b]=;
}
dp[][]=; //初始化第一个当放一个钥匙时总数为1
for(int i=;i<(<<n);i++)
{
for(int j=;j<n;j++)
{
if(dp[i][j]==) continue; //也可以用!(i&(1<<j))来判断,但后者会更费时
for(int k=;k<n;k++) //k也可以从0开始
{
if(!d[k][j]) continue; //判断k与j是否能连接
if(i&(<<k)) continue; //判断第K个钥匙是否已算过
dp[i|(<<k)][k]+=dp[i][j];
}
}
}
long long ans=;
for(int i=;i<n;i++)
{
if(d[][i]) ans+=dp[(<<n)-][i];
}
cout<<ans<<endl;
}
return ;
}

状态压缩DP的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

  10. HDU 4511 (AC自动机+状态压缩DP)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...

随机推荐

  1. MapKit地图划线

    只要用于获取用户位置都要取得用户授权 #import "ViewController.h" #import <MapKit/MapKit.h> @interface V ...

  2. ITOO 第一个任务,新建界面

    最近我们已经接手了我们传承的的ITOO项目,虽然现在还是学习阶段,但是总是还有一些可以完成的东西的.通过需求学习阶段,对照给的原型,逐渐的我们发现了各种各样的问题,有的是根据个人性格有关,有的是对需求 ...

  3. SQLServer 维护脚本分享(10)索引

    --可添加索引的字段 migs.user_seeks,migs.avg_total_user_cost,migs.avg_user_impact,migs.last_user_seek ,mid.st ...

  4. lr中定义字符串变量

    需要注意的是数组必须定义为固定的长度,如:char chary[20]: 5C2o"Go!\gm  nHH0 数组的最大长度为32064(32K),否则会出现“too many variab ...

  5. C语言中float如何存储?

    float 内存如何存储的 类型 存储位数 总位数 偏移值 (offset) 数符(S) 阶码(E) 尾数(M) 短实数(float) 1 8 23 32 127 长实数(double) 1 11 5 ...

  6. java&Protocol Buffers

    ps: Protocol Buffers简称PB PB 安装配置 下载 PB: 在 PB 官网,下载最新版(或者其他版本)PB,这里为了与 Java 项目中的 PB Maven 依赖版本一致,使用 P ...

  7. DSP using MATLAB 示例Example3.23

    代码: % Discrete-time Signal x1(n) : Ts = 0.0002 Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x1 = exp(-1000 ...

  8. Sprint回顾_团队听诊器

  9. js事件绑定细节说明

    javascript绑定事件: 经常用jQuery去写,时间长了对原生态的js事件绑定的知识会慢慢淡化或者遗忘了,必须翻出来再次总结,今天再次把js原生态事件的处理做个总结. 从最初开始,谁刚接触ja ...

  10. 【POJ2096】Collecting Bugs 期望

    [POJ2096]Collecting Bugs Description Ivan is fond of collecting. Unlike other people who collect pos ...