Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 709    Accepted Submission(s): 245

Problem 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
 
Source
 
/*
已经给了时间看题解了,正式练习一道题三天之内不准再看题解!
*/
#include<iostream>
#include<string.h>
#include<stdio.h>
#define N (1<<18)+5
#define M 20
#define INF 0x3f3f3f3f
using namespace std;
long long dp[N][M],g[M][M],n,m;//dp[i][j]表示在i个状态下,你到达j这个点的方案数
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%lld%lld",&n,&m)!=EOF)
{
memset(dp,0,sizeof dp);
memset(g,0,sizeof g);
int a,b;
for(int i=0;i<m;i++)
{
scanf("%lld%lld",&a,&b);
g[a-1][b-1]=g[b-1][a-1]=1;
}
int tol=(1<<n);
dp[1][0]=1;
for(int i=1;i<tol;i++)
{
for(int j=0;j<n;j++)//枚举你要经过的中间点
{
if(dp[i][j]==0) continue;//上一个状态没有方案数的讨论没意义
//cout<<"ok"<<endl;
for(int k=1;k<n;k++)//枚举你这个状态最终要到达的终点
{
if(!(i&(1<<k))&&g[j][k])
{
//cout<<"ok"<<endl;
dp[i|(1<<k)][k]+=dp[i][j];
}
}
}
}
long long s=0;
for(int i=0;i<n;i++)
{
//cout<<dp[tol-1][i]<<" ";
if(g[0][i])
s+=dp[tol-1][i];
}
//cout<<endl;
printf("%lld\n",s);
}
return 0;
}

  

 

hdu 3091 Necklace(状态压缩类似于TSP问题)的更多相关文章

  1. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. hdu 4272 LianLianKan 状态压缩

      LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  6. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  7. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  8. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

  9. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

随机推荐

  1. GCD之异步同步体会

    前面的博文也有写到同步异步,可能是看他人的博文,自己没有实验,感觉理解不深,所以就敲了些代码比较一下串行.并行分别对应的同步.异步. 1.首先创建串行.并行线程队列 1 2 dispatch_queu ...

  2. java 对象转型

    一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...

  3. Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动

    在启动VS2010项目时,遇到如图所示"Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动"的问题. 原因是VS2010安装路径(我的是D: ...

  4. React获得真实的DOM操作

    真实的DOM操作 ------------------------------------------------------------------------------------------- ...

  5. snsapi_base和snsapi_userinfo

    1.以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的.用户感知的就是直接进入了回调页(往往是业务页面) 2.以snsap ...

  6. Visual Studio + Qt开发环境搭建

    1. 安装Visual Studio 2015 Visual Studio 2015下载地址如下,安装比较常规,不做介绍. Visual Studio Enterprise 2015 with Upd ...

  7. 如何安装和配置 Rex-Ray?- 每天5分钟玩转 Docker 容器技术(74)

    Rex-Ray 是一个优秀的 Docker volume driver,本节将演示其安装和配置方法. Rex-Ray 以 standalone 进程的方式运行在 Docker 主机上,安装方法很简单, ...

  8. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  9. EnCase missed some usb activities in the evidence files

    My friend is a developer and her colleague May was suspected of stealing the source code of an impor ...

  10. C# 7.0 新特性:本地方法

    C# 7.0:本地方法 VS 2017 的 C# 7.0 中引入了本地方法,本地方法是一种语法糖,允许我们在方法内定义本地方法.更加类似于函数式语言,但是,本质上还是基于面向对象实现的. 1. 本地方 ...